Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/moreartyjs/moreartyjs
Morearty.js - centralized state management for React in pure JavaScript
https://github.com/moreartyjs/moreartyjs
Last synced: about 1 month ago
JSON representation
Morearty.js - centralized state management for React in pure JavaScript
- Host: GitHub
- URL: https://github.com/moreartyjs/moreartyjs
- Owner: moreartyjs
- License: apache-2.0
- Archived: true
- Created: 2014-07-10T17:05:52.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-12-06T00:06:18.000Z (about 7 years ago)
- Last Synced: 2024-10-03T20:46:38.209Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 2.3 MB
- Stars: 669
- Watchers: 26
- Forks: 41
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-react - morearty.js - Better state management for React in pure JavaScript
- awesome-state - moreartyjs
README
[npm-url]: https://npmjs.org/package/morearty
[npm-image]: http://img.shields.io/npm/v/morearty.svg?style=flat# Morearty.js
[![NPM version][npm-image]][npm-url] [![Build Status](https://travis-ci.org/moreartyjs/moreartyjs.svg?branch=master)](https://travis-ci.org/moreartyjs/moreartyjs) [![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/moreartyjs/moreartyjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![devDependency Status](https://david-dm.org/moreartyjs/moreartyjs/dev-status.svg)](https://david-dm.org/moreartyjs/moreartyjs#info=devDependencies)* [Introduction](#introduction)
* [Download](#download)
* [Dependencies](#dependencies)
* [Current status](#current-status)
* [Documentation](#documentation)
* [Usage](#usage)
* [TodoMVC](#todomvc)
* [App component](#app-component)
* [Header component](#header-component)
* [TodoList component](#todolist-component)
* [TodoItem component](#todoitem-component)
* [Footer component](#footer-component)
* [Starting the application](#starting-the-application)
* [Principal differences from raw React](#principal-differences-from-raw-react)
* [Flux implementation](#flux-implementation)
* [React Native support](#react-native-support)
* [Care to help?](#care-to-help)
* [Building Morearty](#building-morearty)
* [Credits](#credits)# Introduction
**Morearty.js** is a thin layer on top of [React](http://facebook.github.io/react/index.html) (implemented as a mixin) providing better state management facilities in the manner of [Om](https://github.com/swannodette/om) but written in pure JavaScript.
Underneath, Morearty leverages immutable data structures, provided by Facebook's [Immutable](https://github.com/facebook/immutable-js) library, which hold the state of an application. That state is described by a single [Binding](https://rawgit.com/moreartyjs/moreartyjs/master/doc/Binding.html) object and all state transitions are performed through it. When an application component needs to delegate a part of its state to a sub-component, it can create a [sub-binding](https://rawgit.com/moreartyjs/moreartyjs/master/doc/Binding.html#sub) which points to a nested location within the global state and is fully synchronized with the original binding. This way every component knows only what it should know and the entire state is effectively encapsulated. Morearty detects any changes automatically and triggers re-rendering. Moreover, each component gets a correctly defined [shouldComponentUpdate](http://facebook.github.io/react/docs/component-specs.html#updating-shouldcomponentupdate) method that compares the component's state using straightforward JavaScript strict equals operator `===`. So, only the components whose state was altered are re-rendered.
Morearty puts state updates in a render queue and applies them asynchronously in [requestAnimationFrame](http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/) in one pass, falling back to `setTimeout` when `requestAnimationFrame` is not available. This dramatically simplifies reasoning about the application and improves performance.
See [documentation](#documentation) for more info.
# Download
Browser, AMD, Node.js environments are supported. You can get [production](https://raw.githubusercontent.com/moreartyjs/moreartyjs/master/dist/morearty.min.js) or [development](https://raw.githubusercontent.com/moreartyjs/moreartyjs/master/dist/morearty.js) versions. Or just `npm install morearty`.
# Dependencies
Morearty requires React version 0.13 or higher ([download](http://facebook.github.io/react/downloads.html)) and Immutable 3.7 and above ([download](https://github.com/facebook/immutable-js/releases)). **Both should be available as global variables with names `React` and `Immutable` unless you're using NPM.** Require.js users can do something like:
```javascript
require.config({
paths: {
react: 'path/to/react',
immutable: 'path/to/immutable'
}
});require(['react', 'immutable'], function (React, Imm) {
window.React = React;
window.Immutable = Imm;require(['component/Bootstrap'], function (Bootstrap) {
React.renderComponent(
Bootstrap(),
document.getElementById('root')
);
});
});
```# Current status
**Morearty** 0.7 series changes:
* Support React 0.14 (starting from `0.7.25`).
* React Native [support](#react-native-support) (thanks to @gilbox).
* Support React 0.13 and Immutable 3.7.
* Asynchronous rendering is the default, synchronous mode is no longer supported.
* Support [cancelling](https://rawgit.com/moreartyjs/moreartyjs/master/doc/TransactionContext.html#cancel) transactions.
* Support `this.addBindingListener(...)` in components for component lifecycle bounded listeners creation. Just listen for changes, all required cleanup is performed in `componentWillUnmount` automatically.
* Support `renderOnce` configuration parameter useful to ensure rendering is performed only once. Other server rendering corrections.
* `Context.bootstrap` helper method simplifying application bootstrapping.
* Support dynamic bindings ([#36](https://github.com/moreartyjs/moreartyjs/issues/36)).
* Support passing custom React context ([#37](https://github.com/moreartyjs/moreartyjs/issues/37)).
* Introduced [observed bindings](https://github.com/moreartyjs/moreartyjs/wiki/Authoring-components#using-observed-bindings) and [observed props](https://github.com/moreartyjs/moreartyjs/wiki/Authoring-components#using-observed-props).
* Support IE8. Deprecate `Binding.delete` in favor of `remove`.
* Support `getDefaultMetaState` in components.**Morearty** 0.6 series changes:
* React 0.12 and Immutable 3.0 or higher now required.
* Introduce [bindings meta info](https://github.com/moreartyjs/moreartyjs/wiki/Binding#attaching-meta-information) that allows to store data you don't want to put in the main state, e.g. validation info, history, and so on.
* Generate less garbage during render.
* History module migrated on meta binding API.See [releases page](https://github.com/moreartyjs/moreartyjs/releases) for detailed per-release changes descriptions.
# Documentation
See Wiki [pages](https://github.com/moreartyjs/moreartyjs/wiki) for a thourough explanation of Morearty concepts.
Auto-generated API documentation is available [here](https://rawgit.com/moreartyjs/moreartyjs/master/doc/index.html).
# Usage
To start using Morearty.js add the script to the page or load it with your favorite AMD loader, e.g. [Require.js](http://requirejs.org/), and create Morearty context using [createContext](https://rawgit.com/moreartyjs/moreartyjs/master/doc/Morearty.html#createContext) method:
```javascript
var Ctx = Morearty.createContext({
initialState: {
nowShowing: 'all',
items: [{
title: 'My first task',
completed: false,
editing: false
}]
}
});
```When you create components this way, each acquires a correctly defined `shouldComponentUpdate` method which uses that component's binding (if any) to determine if its state was changed. By default, state is transferred to sub-components in the `binding` attribute and can be retrieved using the `getDefaultBinding` method.
# TodoMVC
To continue this introduction, [TodoMVC](http://todomvc.com/) implementation based on Morearty.js will be used ([repository](https://github.com/moreartyjs/todomvc-moreartyjs), [application](https://rawgit.com/moreartyjs/todomvc-moreartyjs/master/index.html)). You should have some previous React knowledge to follow painlessly, only Morearty-specific parts will be described.## App component
Let's now define main application module `App`:```javascript
var NOW_SHOWING = Object.freeze({ ALL: 'all', ACTIVE: 'active', COMPLETED: 'completed' });var App = React.createClass({
displayName: 'App',mixins: [Morearty.Mixin],
componentDidMount: function () {
var binding = this.getDefaultBinding();
Router({
'/': binding.set.bind(binding, 'nowShowing', NOW_SHOWING.ALL),
'/active': binding.set.bind(binding, 'nowShowing', NOW_SHOWING.ACTIVE),
'/completed': binding.set.bind(binding, 'nowShowing', NOW_SHOWING.COMPLETED)
}).init();
},render: function () {
var binding = this.getDefaultBinding();
return (
);
}
});
```Notice that `App` uses `getDefaultBinding` method to retrieve its state binding and delegate it to its children. See `getDefaultBinding` API [doc](https://rawgit.com/moreartyjs/moreartyjs/master/doc/Morearty.Mixin.html#getDefaultBinding) for the discussion of the default binding concept.
## Header component
```javascript
var Header = React.createClass({
displayName: 'Header',
mixins: [Morearty.Mixin],componentDidMount: function () {
this.refs.newTodo.getDOMNode().focus(); // focus on show
},onAddTodo: function (event) {
var title = event.target.value;
if (title) {
this.getDefaultBinding().update('items', function (todos) { // add new item
return todos.push(Immutable.Map({
id: currentId++,
title: title,
completed: false,
editing: false
}));
});
event.target.value = '';
}
},render: function () {
return (
todos
);
}
});
```In `onAddTodo` method component state is [updated](https://rawgit.com/moreartyjs/moreartyjs/master/doc/Binding.html#update) by appending new TODO item to the list. `render` method output custom `input` component version suitable for [rendering in requestAnimationFrame](https://github.com/moreartyjs/moreartyjs#requestanimationframe-support).
## TodoList component
```javascript
var TodoList = React.createClass({
displayName: 'TodoList',mixins: [Morearty.Mixin],
onToggleAll: function (event) {
var completed = event.target.checked;
this.getDefaultBinding().update('items', function (items) {
return items.map(function (item) {
return item.set('completed', completed);
});
});
},render: function () {
var binding = this.getDefaultBinding();
var nowShowing = binding.get('nowShowing');
var itemsBinding = binding.sub('items');
var items = itemsBinding.get();var isShown = function (item) {
switch (nowShowing) {
case NOW_SHOWING.ALL:
return true;
case NOW_SHOWING.ACTIVE:
return !item.get('completed');
case NOW_SHOWING.COMPLETED:
return item.get('completed');
}
};var renderTodo = function (item, index) {
var itemBinding = itemsBinding.sub(index);
return isShown(item) ? : null;
};var allCompleted = !items.find(function (item) {
return !item.get('completed');
});return (
{
items.count() ?
:
null
}
- { items.map(renderTodo).toArray() }
);
}
});
```
`onToggleAll` callback sets `completed` property on all items. Note how state is transferred to the children: only the relevant sub-state is passed using [sub](https://rawgit.com/moreartyjs/moreartyjs/master/doc/Binding.html#sub) method which creates a sub-binding pointing deeper into global state. So, TODO item can only access and modify its own cell, and the rest of application state is protected from incidental modification. [val](https://rawgit.com/moreartyjs/moreartyjs/master/doc/Binding.html#val) method allows to retrieve the value stored in the binding or in its sub-path.
## TodoItem
```javascript
var TodoItem = React.createClass({
displayName: 'TodoItem',
mixins: [Morearty.Mixin],
componentDidUpdate: function () {
var ctx = this.getMoreartyContext();
if (ctx.isChanged(this.getDefaultBinding().sub('editing'))) {
var node = this.refs.editField.getDOMNode();
node.focus();
node.setSelectionRange(0, node.value.length);
}
},
onToggleCompleted: function (event) {
this.getDefaultBinding().set('completed', event.target.checked);
},
onToggleEditing: function (editing) {
this.getDefaultBinding().set('editing', editing);
},
onEnter: function (event) {
this.getDefaultBinding().atomically()
.set('title', event.target.value)
.set('editing', false)
.commit();
},
render: function () {
var binding = this.getDefaultBinding();
var item = binding.get();
var liClass = React.addons.classSet({
completed: item.get('completed'),
editing: item.get('editing')
});
var title = item.get('title');
return (
{ title }
);
}
});
```
Here component title is written to the global state using [set](https://rawgit.com/moreartyjs/moreartyjs/master/doc/Callback.html#set) helper when text in changed. To remove the item no callback needs to be passed from the parent: item component just calls Binding's [remove](https://rawgit.com/moreartyjs/moreartyjs/master/doc/Binding.html#remove) method which removes it from the list of items. In `onEnter` method transaction is used to prevent re-rendering between state transitions. It effectively notifies global listeners once on [commit](https://rawgit.com/moreartyjs/moreartyjs/master/doc/TransactionContext.html#commit).
## Footer component
```javascript
var Footer = React.createClass({
displayName: 'Footer',
mixins: [Morearty.Mixin],
onClearCompleted: function () {
this.getDefaultBinding().update('items', function (items) {
return items.filter(function (item) {
return !item.get('completed');
});
});
},
render: function () {
var binding = this.getDefaultBinding();
var nowShowing = binding.get('nowShowing');
var items = binding.get('items');
var completedItemsCount = items.reduce(function (acc, item) {
return item.get('completed') ? acc + 1 : acc;
}, 0);
return (
{ items.count() - completedItemsCount + ' items left' }
{
completedItemsCount ?
{ 'Clear completed (' + completedItemsCount + ')' }
:
null
}
);
}
});
```
Nothing special here so let's jump straight to...
## Starting the application
```javascript
var Bootstrap = Ctx.bootstrap(App); // will pass root binding to App
React.render(
,
document.getElementById('root')
);
```
`Ctx.bootstrap` method creates Morearty application bootstrap component which is then passed to React render routine.
## Principal differences from raw React
You can compare this Morearty-based TodoMVC implementation to the official React [version](https://github.com/tastejs/todomvc/tree/gh-pages/architecture-examples/react). Main highlights are:
* No callbacks are passed to sub-components. This becomes especially useful when you find yourself trying to transfer a callback to a component's grand-children (you may never know how your DOM may be restructured after a redesign). There is nothing inherently wrong in passing callbacks to sub-components, but in many cases this can be avoided.
* No hacks in code simulating immutable state and other tricks (look at the comments within React version sources).
* Reasoning about the application is much simpler!
* Each component gets a `shouldComponentUpdate` method, no need to define it manually (but you can if you like).
* Less code.
# Flux implementation
[z3tsu](https://github.com/z3tsu) provided Flux version of Todo-MVC based on [Reflux](https://github.com/spoike/refluxjs): [z3tsu/todomvc-morearty-reflux](https://github.com/z3tsu/todomvc-morearty-reflux).
# React Native support
Starting from version *0.7.16* running on React Native is supported:
```javascript
var Morearty = require('morearty/native');
```
# Care to help?
Feel free to [provide](https://github.com/moreartyjs/moreartyjs/issues) ideas, suggestions, enhancements, documentation improvements. Any feedback or input is highly appreciated. Morearty development is currently driven mostly by *feature requests*.
## Building Morearty
Morearty uses NPM scripts for building: `npm run ` where `command` is one of:
* test (run tests);
* build (run tests and build dist files);
* doc (generate documentation).
# Credits
* Alexander Semenov @Tvaroh (author)
* Marat Bektimirov @mbektimirov (collaborator)
* Tim Griesser @tgriesser (collaborator)
* Pavel Birukov @r00ger (collaborator)
* Gil Birman @gilbox (collaborator)
* Valentine Valyaeff @valff (collaborator)