From 6b6792994af0d5c85a4a6a4312303b7558ca5691 Mon Sep 17 00:00:00 2001 From: "Rico Sta. Cruz" Date: Thu, 26 Feb 2015 03:09:45 +0800 Subject: [PATCH] Update flux --- flux.md | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/flux.md b/flux.md index 38cc89460..fe7b3f2b1 100644 --- a/flux.md +++ b/flux.md @@ -22,18 +22,18 @@ var Dispatcher = require('flux').Dispatcher; d = new Dispatcher(); -// send a payload -d.dispatch({ a: 'aaa', ... }; +// send +d.dispatch({ action: 'edit', ... }; -// receive payloads +// receive token = d.register(function (payload) { - payload.a === 'aaa' + payload.action === 'edit' }) ``` ### Ensuring proper order -You can ensure one listener is fired after another using `.waitFor()`. +With multiple listeners, you can ensure one is fired after another using `.waitFor()`. ```js token1 = d.register(...); @@ -80,7 +80,7 @@ var TodoStore = { todos: {} }; ``` ### Events -Sometimes they're eventemitters, too. +Sometimes they're eventemitters, too. Usually it's used to emit `change` events for views to pick up. ```js var TodoStore = assign({}, EventEmitter.prototype, { @@ -137,22 +137,47 @@ Components can listen to Dispatchers. var TodoApp = React.createClass({ componentDidMount() { - this.token = AppDispatcher.register(function (payload) { + this.token = AppDispatcher.register((payload) => { switch (payload.action) { case 'tab.change': + this.render(); // ... } }); + }, + + componentDidUnmount() { + AppDispatcher.unregister(this.token); } }); ``` +Or to Stores. + +```js +{ + componentDidMount() { + TodoStore.on('change', this.onChange); + }, + + componentDidUnmount() { + TodoState.removeListener('change', this.onChange); + }, + + onChange(data) { + // ... + } +} +``` + ---- ### Also see * [Dispatcher API][dispatcher] * [React cheatsheet](react.html) +* [Dispatcher.js source](https://github.com/facebook/flux/blob/master/src/Dispatcher.js) +* [Flux-todomvc explanation](https://github.com/facebook/flux/tree/master/examples/flux-todomvc) [dispatcher]: http://facebook.github.io/flux/docs/dispatcher.html \ No newline at end of file