今天我们来结合一个简单的Demo来讲解Flux框架,让大家了解Flux框架的真面目。
先上一张比较漂亮的图(对漂亮的图,总是没有抵抗力:-) )。
  createNewItem: function (event) {
    ButtonActions.addNewItem('new item');
  },
  var items = this.state.items;
  var itemHtml = items.map(function (listItem, i) {
    return <li key={i}>{listItem}</li>;
  });
  render: function() {
	  return 
		  <div>
		    <ul>{itemHtml}</ul>
		    <button onClick={this.createNewItem}>New Item</button>
		  </div>;
  }var ButtonActions = {
  addNewItem: function (text) {
    AppDispatcher.dispatch({
      actionType: 'ADD_NEW_ITEM',
      text: text
    });
  },
};ADD_NEW_ITEM
UPDATE_ITEM
DELETE_ITEM
AppDispatcher.register(function (action) {
  switch(action.actionType) {
    case 'ADD_NEW_ITEM':
      ListStore.addNewItemHandler(action.text);
      ListStore.emitChange();
      break;
    case 'UPDATE_ITEM':
      ...
      break;
    case 'DELETE_ITEM':
      ...
      break;
    default:
  }
})Dispatcher收到ADD_NEW_ITEM动作,就会执行回调函数,对ListStore进行操作。var EventEmitter = require('events').EventEmitter;
var assign = require('object-assign');
var ListStore = assign({}, EventEmitter.prototype, {
  items: [],
  getAll: function () {
    return this.items;
  },
  addNewItemHandler: function (text) {
    this.items.push(text);
  },
  emitChange: function () {
    this.emit('change');
  },
  addChangeListener: function(callback) {
    this.on('change', callback);
  },
  removeChangeListener: function(callback) {
    this.removeListener('change', callback);
  }
});
module.exports = ListStore;
  componentDidMount: function() {
    ListStore.addChangeListener(this._onChange);
  },
  componentWillUnmount: function() {
    ListStore.removeChangeListener(this._onChange);
  },
  _onChange: function () {
    this.setState({
      items: ListStore.getAll()
    });
  },《React-Native系列》22、 Flux框架Demo详解
原文:http://blog.csdn.net/codetomylaw/article/details/52287614