以äžã®ç¿»èš³ã§ã¯ããã€ãã³ãããšããçšèªã¯ãéæµããã®ã¢ã¯ã·ã§ã³ããšããçšèªã«äœ¿çšãããããšããããæèã«ãã£ãŠã¯ãã¢ã¯ã·ã§ã³ããšããçšèªã«äœ¿çšãããããšããããŸãã ãã以äžã®ç¿»èš³ã®æåãèŠã€ããããšãã§ããŸããã§ããã ãªãã·ã§ã³ãããã°ãã³ã¡ã³ãã§ææ¡ãåŸ ã£ãŠããŸãã
埩ç¿
RefluxJSã¯ãFacebookã®Fluxã³ã³ã»ããã䜿çšããŠãã¢ããªã±ãŒã·ã§ã³ã§åæ¹åããŒã¿ã¹ããªãŒã ãæäŸããã·ã³ãã«ãªã©ã€ãã©ãªã§ãã
Fluxã¢ãŒããã¯ãã£ã®æŠèŠã«ã€ããŠã¯ã ãã¡ããã芧ãã ãã ããã®èšäºã§ã¯ãMVCã¢ãŒããã¯ãã£ããåæ¹åããŒã¿ã¹ããªãŒã ã«ç§»è¡ãããé¢æ°ããã°ã©ãã³ã°ã®æ©èœãå€§å¹ ã«äœ¿çšãã代æ¿ãªãã·ã§ã³ã«ã€ããŠèª¬æããŸãã
âââââââââââ ââââââââââ âââââââââââââââââââ â Actions âââââââ>â Stores âââââââ>â View Components â âââââââââââ ââââââââââ âââââââââââââââââââ ^ â ââââââââââââââââââââââââââââââââââââââââ
ãã¿ãŒã³ã¯ãã¢ã¯ã·ã§ã³ãšã¢ã¯ã·ã§ã³ã¹ãã¢ã§æ§æãããŸãã ã¢ã¯ã·ã§ã³ã¯ãã¹ãã¬ãŒãžããããžã¥ã¢ã«ã³ã³ããŒãã³ããžã®ã€ãã³ããéããŠããŒã¿ã®ç§»åãéå§ããŸãã ãŠãŒã¶ãŒãäœããããå Žåãã¢ã¯ã·ã§ã³ã䜿çšããŠã€ãã³ããçæãããŸãã ããŒã¿ãŠã§ã¢ããŠã¹ã¯ãã®ã€ãã³ãã«ãµãã¹ã¯ã©ã€ããããŸãã ã€ãã³ããåŠçããå Žåã«ãã£ãŠã¯ãäœããã®ç¬èªã®ã€ãã³ããçæããŸãã
ããšãã°ããŠãŒã¶ãŒãã¢ããªã±ãŒã·ã§ã³ã®ãªã¹ãã®ãã£ã«ã¿ãªã³ã°ãå€æŽããŸããã ãã£ã«ã¿ãŒã³ã³ããŒãã³ãã¯ããã£ã«ã¿ãŒå€æŽã€ãã³ããçæããŸãã ãªããžããªã¯ããã«å¿çããŠãæŽæ°ããããã£ã«ã¿ãŒã䜿çšããŠajaxãªã¯ãšã¹ããå®è¡ããããã«ãµã€ã³ã¢ãããããã¹ãŠã®ãŠãŒã¶ãŒã«ãæäŸãããããŒã¿ã»ãããå€æŽãããããšãéç¥ããŸãã
å 容
- RefluxãšFacebook Fluxã®æ¯èŒ
- äŸ
- èšçœ®
- 䜿çšãã
- ã€ãã³ã
- ä¿ç®¡æœèš
- ReactJSã³ã³ããŒãã³ãã§äœ¿çšãã
- 詳现
- ãšãããŒã°
RefluxãšFacebook Fluxã®æ¯èŒ
RefluxJSãããžã§ã¯ãã®ç®æšã¯ãã¯ã©ã€ã¢ã³ãåŽãšãµãŒããŒåŽã®äž¡æ¹ã§ããããžã§ã¯ããžã®Fluxã¢ãŒããã¯ãã£ã®ããã·ã³ãã«ã§è¿ éãªçµ±åã§ãã ãã ããRefluxJSã®åäœãšåŸæ¥ã®Fluxã¢ãŒããã¯ãã£ãæäŸãããã®ãšã®éã«ã¯ããã€ãã®éãããããŸãã 詳现ã«ã€ããŠã¯ã ãã®ããã°æçš¿ãåç §ããŠãã ããã
Fluxãšã®é¡äŒŒç¹
äžéšã®RefluxJSã®æŠå¿µã¯Fluxã«äŒŒãŠããŸãïŒ
- ã¢ã¯ã·ã§ã³ããããŸã
- ããŒã¿ãŠã§ã¢ããŠã¹ããããŸã
- ããŒã¿ã¯äžæ¹åã«ã®ã¿ç§»åããŸãã
ãã©ãã¯ã¹ãšã®éã
RefluxJSã¯ãFluxã³ã³ã»ããã®æ¹è¯çã§ãããæ©èœçãªã¢ã¯ãã£ãããã°ã©ãã³ã°ã«ããåçã§äœ¿ãããããã®ã§ãã
- Fluxã®ã·ã³ã°ã«ãã³ã§ãã£ããã£ã¹ãããã£ã¯ãRefluxJSã«ã¯ãããŸããã 代ããã«ãåã€ãã³ãïŒã¢ã¯ã·ã§ã³ïŒã¯ç¬èªã®ãã£ã¹ãããã£ã§ãã
- ã¢ã¯ã·ã§ã³ããµãã¹ã¯ã©ã€ãã§ãããããã¹ãã¢ã¯é¢åãªswitchã¹ããŒãã¡ã³ãã䜿çšããŠããšãã«ãã¬ãããåé¢ããããšãªãããããçŽæ¥å®è¡ã§ããŸãã
- Vaultã¯ãä»ã®Vaultã«ãµãã¹ã¯ã©ã€ãã§ããŸãã ã€ãŸããmap-reduceããŒã¿ãéçŽããŠåŠçãããªããžããªãäœæããããšãå¯èœã«ãªããŸãã
- waitForïŒïŒã®åŒã³åºãã¯åé€ãããŸããã 代ããã«ãããŒã¿åŠçãé 次ãŸãã¯äžŠè¡ããŠå®è¡ã§ããŸãã
- ããŒã¿ãéçŽãããªããžããªïŒäžèšåç §ïŒã¯ãä»ã®ãªããžããªããµãã¹ã¯ã©ã€ãããã¡ãã»ãŒãžãé 次åŠçã§ããŸã
- ä»ã®ã€ãã³ããåŠçãããã®ãåŸ ã€ã«ã¯ãjoinïŒïŒã¡ãœããã䜿çšã§ããŸã
- RefluxJSã¢ã¯ã·ã§ã³ã¯ããµãã¹ã¯ã©ã€ããããã¹ãŠã®ãŠãŒã¶ãŒã«å¿ èŠãªããŒã¿ãéä¿¡ããæ©èœã§ãããããç¹å¥ãªã¢ã¯ã·ã§ã³ãã¡ã¯ããªã¯ãŸã£ããå¿ èŠãããŸããã
äŸ
ããã€ãã®äŸã¯ã次ã®ã¢ãã¬ã¹ã«ãããŸãã
- RefluxJSã®ToDoãªã¹ãã®ãã©ãããäœæããäŸ
- echenleyãææããHackerNewsã¯ããŒã³
- Pythonã䜿çšããŠèå ã«ããããã¯ãšã³ããå®è£ ããå¥ã®TODOãããžã§ã¯ã
èšçœ®
çŸæç¹ã§ã¯ãRefluxJSã¯npmãŸãã¯bowerã䜿çšããŠã€ã³ã¹ããŒã«ã§ããŸãã
NPM
npmã䜿çšããŠã€ã³ã¹ããŒã«ããã«ã¯ã次ã®ã³ãã³ããå®è¡ããŸãã
npm install reflux
ããŠã¢ãŒ
bowerã䜿çšããŠã€ã³ã¹ããŒã«ããã«ã¯ïŒ
bower install reflux
ES5
Reactãšåæ§ã«ãRefluxJSã¯å€ããã©ãŠã¶çšã«es5-shimãå¿ èŠãšããŸãã ããã§ãããåãããšãã§ããŸã
䜿çšãã
å®å šãªäŸã¯ããã«ãããŸã ã
ã¢ã¯ã·ã§ã³ãäœæãã
ã¢ã¯ã·ã§ã³ã¯ `Reflux.createActionïŒïŒ`ãåŒã³åºãããšã§äœæãããŸãã ãªãã·ã§ã³ã®ãªã¹ãããã©ã¡ãŒã¿ãŒãšããŠæž¡ãããšãã§ããŸãã
var statusUpdate = Reflux.createAction(options);
ã¢ã¯ã·ã§ã³ãªããžã§ã¯ãã¯ãã¡ã³ã¯ã¿ãªã®ã§ããªããžã§ã¯ããé¢æ°ãšããŠåç §ããããšã§åŒã³åºãããšãã§ããŸãïŒ
statusUpdate(data); // statusUpdate, data statusUpdate.triggerAsync(data); // ,
`options.sync`ãtrueã«èšå®ãããŠããå Žåãã€ãã³ãã¯åææäœãšããŠããªã¬ãŒãããŸãã ãã®èšå®ã¯ãã€ã§ãå€æŽã§ããŸãã ãã以éã®ãã¹ãŠã®åŒã³åºãã§ã¯ãèšå®å€ã䜿çšãããŸãã
å€æ°ã®ã¢ã¯ã·ã§ã³ãç°¡åã«äœæããã«ã¯ã次ã®ããã«ããŸãã
var Actions = Reflux.createActions([ "statusUpdate", "statusEdited", "statusAdded" ]); // Actions , createActions(). // Actions.statusUpdate();
ã¢ã¯ã·ã§ã³ã䜿çšããéåæäœæ¥
éåæçã«åŠçã§ããã€ãã³ãïŒAPIåŒã³åºããªã©ïŒã«ã¯ãããã€ãã®ç°ãªããªãã·ã§ã³ããããŸãã æãäžè¬çãªã±ãŒã¹ã§ã¯ãæ£åžžçµäºãšãšã©ãŒãèæ ®ããŸãã ãã®ããªã¢ã³ãã§ããŸããŸãªã€ãã³ããäœæããã«ã¯ã `options.children`ã䜿çšã§ããŸãã
// 'load', 'load.completed' 'load.failed' var Actions = Reflux.createActions({ "load": {children: ["completed","failed"]} }); // 'load', , // , failed completed Actions.load.listen( function() { // . // this someAsyncOperation() .then( this.completed ) .catch( this.failed ); });
ãã®å Žåãç¹å¥ãªãªãã·ã§ã³ïŒ `options.asyncResult`ããããŸãã 次ã®ã¢ã¯ã·ã§ã³å®çŸ©ã¯åçã§ãã
createAction({ children: ["progressed","completed","failed"] }); createAction({ asyncResult: true, children: ["progressed"] });
次ã®ã¡ãœããã¯ããå®äºããšã倱æããšããåã¢ã¯ã·ã§ã³ãèªåçã«åŒã³åºãããã«äœ¿çšã§ããŸãã
- `promise`-ãã©ã¡ãŒã¿ãŒãšããŠãpromiseãªããžã§ã¯ããæ³å®ããthenïŒïŒ` and` catchïŒïŒ `ã䜿çšããŠããã®promiseã®` complete`ããã³ `failed`åŒã³åºãããã€ã³ãããŸãã
- `listenAndPromise`-ãã©ã¡ãŒã¿ãŒãšããŠãpromiseãªããžã§ã¯ããè¿ãé¢æ°ãå¿ èŠã§ãã ã€ãã³ããçºçãããšãããïŒé¢æ°ãè¿ããpromiseãªããžã§ã¯ãïŒãåŒã³åºãããŸãã ãããã£ãŠãpromiseã® `thenïŒïŒ`ããã³ `catchïŒïŒ`ã«ãã£ãŠãcompletedããã³failedãèªåçã«åŒã³åºãããŸãã
次ã®3ã€ã®å®çŸ©ã¯åçã§ãã
asyncResultAction.listen( function(arguments) { someAsyncOperation(arguments) .then(asyncResultAction.completed) .catch(asyncResultAction.failed); }); asyncResultAction.listen( function(arguments) { asyncResultAction.promise( someAsyncOperation(arguments) ); }); asyncResultAction.listenAndPromise( someAsyncOperation );
çŽæãšããŠã®éåæã¢ã¯ã·ã§ã³
éåæã¢ã¯ã·ã§ã³ã¯ãããã¹ãšããŠäœ¿çšã§ããŸãã ããã¯ãã¬ã³ããªã³ã°ã®åã«äœããã®ã€ãã³ãã®æ£åžžãªå®äºïŒãŸãã¯æªå®äºïŒãåŸ ã€å¿ èŠãããå Žåã«ããµãŒããŒã§ã®ã¬ã³ããªã³ã°ã«ç¹ã«äŸ¿å©ã§ãã
ã¢ã¯ã·ã§ã³ãšã¹ãã¬ãŒãžããããAPIãªã¯ãšã¹ããå®è¡ããå¿ èŠããããšããŸãïŒ
// `completed` & `failed` "" var makeRequest = Reflux.createAction({ asyncResult: true }); var RequestStore = Reflux.createStore({ init: function() { this.listenTo(makeRequest, 'onMakeRequest'); }, onMakeRequest: function(url) { // , `request` - - HTTP request(url, function(response) { if (response.ok) { makeRequest.completed(response.body); } else { makeRequest.failed(response.error); } }) } });
ãã®å ŽåããµãŒããŒã§ãããã¹ã䜿çšããŠãªã¯ãšã¹ããå®è¡ããäœããã¬ã³ããªã³ã°ããããšã©ãŒãè¿ãããšãã§ããŸãã
makeRequest('/api/something').then(function(body) { // Render the response body }).catch(function(err) { // Handle the API error object });
ã€ãã³ãããã¯
åã€ãã³ãã«äœ¿çšã§ããããã¯ãããã€ããããŸãã
- `preEmit`-ã¢ã¯ã·ã§ã³ãã€ãã³ãæ å ±ããµãã¹ã¯ã©ã€ããŒã«æž¡ãåã«åŒã³åºãããŸãã åŒæ°ãšããŠãããã¯ã¯ã€ãã³ãã®ãã£ã¹ãããã«äœ¿çšãããåŒæ°ãåãåããŸãã ããã¯ãæªå®çŸ©ä»¥å€ãè¿ãå Žåãæ»ãå€ã¯shouldEmitããã¯ã®ãã©ã¡ãŒã¿ãŒãšããŠäœ¿çšãããéä¿¡ãããããŒã¿ã眮ãæããŸã
- ãshouldEmitã-ãpreEmitãã®åŸã§åŒã³åºãããŸãããã¢ã¯ã·ã§ã³ãã€ãã³ãæ å ±ããµãã¹ã¯ã©ã€ããŒã«æž¡ãåã«ã ããã©ã«ãã§ã¯ããã®ãã³ãã©ãŒã¯trueãè¿ããããŒã¿ã®éä¿¡ãèš±å¯ããŸãã ããšãã°ããã®åäœããªãŒããŒã©ã€ãããŠãåŒæ°ã確èªããã€ãã³ãããã§ãŒã³ã«éä¿¡ãããã©ããã決å®ã§ããŸãã
䜿çšäŸïŒ
Actions.statusUpdate.preEmit = function() { console.log(arguments); }; Actions.statusUpdate.shouldEmit = function(value) { return value > 0; }; Actions.statusUpdate(0); Actions.statusUpdate(1); // : 1
ã¢ã¯ã·ã§ã³ã宣èšãããšãã«ãããã¯ãçŽæ¥å®çŸ©ã§ããŸãã
var action = Reflux.createAction({ preEmit: function(){...}, shouldEmit: function(){...} });
Reflux.ActionMethods
ãã¹ãŠã®ã¢ã¯ã·ã§ã³ã®ãªããžã§ã¯ãã§ã¡ãœãããå®è¡ã§ããããã«ããå¿ èŠãããå Žåã¯ããªããžã§ã¯ã `Reflux.ActionMethods`ãå±éã§ããŸãããã®ãªããžã§ã¯ãã¯ãäœææã«ãã¹ãŠã®ã¢ã¯ã·ã§ã³ãšèªåçã«æ··åãããŸãã
䜿çšäŸïŒ
Reflux.ActionMethods.exampleMethod = function() { console.log(arguments); }; Actions.statusUpdate.exampleMethod('arg1'); // : 'arg1'
ã¹ãã¬ãŒãžäœæ
ãªããžããªã¯ã ReactJSã³ã³ããŒãã³ãã¯ã©ã¹ ïŒ `React.createClass`ïŒãšã»ãŒåãæ¹æ³ã§äœæãããŸã-ãªããžããªã®ãã©ã¡ãŒã¿ãŒãå®çŸ©ãããªããžã§ã¯ãã` Reflux.createStore`ã¡ãœããã«æž¡ãããšã«ãã£ãŠã listenTo`ã®ç¬èªã®ãªããžããªã¡ãœãããåŒã³åºãããšã§ããã¹ãŠã®ã€ãã³ããã³ãã©ãŒããªããžããªã® `init`ã¡ãœããã§åæåã§ããŸãã
// var statusStore = Reflux.createStore({ // init: function() { // statusUpdate this.listenTo(statusUpdate, this.output); }, // , output: function(flag) { var status = flag ? 'ONLINE' : 'OFFLINE'; // , this.trigger(status); } });
äžèšã®äŸã§ã¯ã `statusUpdate`ã¢ã¯ã·ã§ã³ãåŒã³åºããããšãéä¿¡äžã«æž¡ããããã¹ãŠã®ãã©ã¡ãŒã¿ãŒãšãšãã«` output`ã¹ãã¬ãŒãžã¡ãœãããåŒã³åºãããŸãã ããšãã°ãã€ãã³ãã `statusUpdateïŒtrueïŒ`ãåŒã³åºãããšã«ãã£ãŠãã£ã¹ããããããå Žåã `true`ãã©ã°ã` output`é¢æ°ã«æž¡ãããŸãã ãã®åŸããªããžããªèªäœãã¢ã¯ã·ã§ã³ãšããŠæ©èœãããµãã¹ã¯ã©ã€ããŒã®ãã¹ããŒã¿ã¹ãããŒã¿ã«æž¡ãããŸãã
ã¹ãã¬ãŒãžèªäœãã€ãã³ãã®ãã£ã¹ãããã®ã€ãã·ãšãŒã¿ãŒã§ããããããpreEmitãããã³ãshouldEmitãããã¯ããããŸãã
Reflux.StoreMethods
ç¹å®ã®ã¡ãœããã»ããããã¹ãŠã®ãªããžããªã§ããã«äœ¿çšã§ããããã«ããå Žåã¯ãäœææã«ãã¹ãŠã®ãªããžããªã«ããã¯ã¹ããã `Reflux.StoreMethods`ãªããžã§ã¯ããå±éã§ããŸãã
䜿çšäŸïŒ
Reflux.StoreMethods.exampleMethod = function() { console.log(arguments); }; statusStore.exampleMethod('arg1'); // : 'arg1'
ã¹ãã¬ãŒãžå ã®äžçŽç©ïŒããã¯ã¹ã€ã³ïŒ
ãªããžã§ã¯ããReactã³ã³ããŒãã³ãã«ããã¯ã¹ããããã«ããããããªããžããªãšããã¯ã¹ã§ããŸãã
var MyMixin = { foo: function() { console.log('bar!'); } } var Store = Reflux.createStore({ mixins: [MyMixin] }); Store.foo(); // "bar!"
äžçŽç©ã¡ãœããã¯ãã¹ãã¬ãŒãžã§å®£èšãããç¬èªã®ã¡ãœãããšåãæ¹æ³ã§å©çšã§ããŸãã ãããã£ãŠãä»»æã®ã¡ãœããã®ãthisãã¯ã¹ãã¬ãŒãžã€ã³ã¹ã¿ã³ã¹ãæããŸãã
var MyMixin = { mixinMethod: function() { console.log(this.foo); } } var Store = Reflux.createStore({ mixins: [MyMixin], foo: 'bar!', storeMethod: function() { this.mixinMethod(); // "bar!" } });
䟿å©ãªããšã«ãåãã€ãã³ãã©ã€ããµã€ã¯ã«ã¡ãœããïŒ `init`ã` preEmit`ã `shouldEmit`ïŒãå®çŸ©ããããã€ãã®äžçŽç©ããªããžããªã«æ··å ¥ããŠããå Žåããããã®ã¡ãœããã¯ãã¹ãŠïŒå®éã«ã¯ReactJSã®ããã«ïŒåŒã³åºãããããšãä¿èšŒãããŸãã
å€æ°ã®ã¢ã¯ã·ã§ã³ã®äŸ¿å©ãªãµãã¹ã¯ãªãã·ã§ã³
éåžžãã¹ãã¢ã®initã¡ãœããã¯ç»é²ããããã¹ãŠã®ã¢ã¯ã·ã§ã³ããµãã¹ã¯ã©ã€ããããããã¹ãã¢ã«ã¯ `listenToMany`ã¡ãœããããããŸãããã®ã¡ãœããã¯ãçæããããã¹ãŠã®ã€ãã³ããæã€ãªããžã§ã¯ããåŒæ°ãšããŠåãåããŸãã ãã®ã³ãŒãã®ä»£ããã«ïŒ
var actions = Reflux.createActions(["fireBall","magicMissile"]); var Store = Reflux.createStore({ init: function() { this.listenTo(actions.fireBall,this.onFireBall); this.listenTo(actions.magicMissile,this.onMagicMissile); }, onFireBall: function(){ // whoooosh! }, onMagicMissile: function(){ // bzzzzapp! } });
...ããã䜿çšã§ããŸãïŒ
var actions = Reflux.createActions(["fireBall","magicMissile"]); var Store = Reflux.createStore({ init: function() { this.listenToMany(actions); }, onFireBall: function(){ // whoooosh! }, onMagicMissile: function(){ // bzzzzapp! } });
åæ§ã®ã³ãŒãã¯ããã¹ãŠã® `actionName`ã¢ã¯ã·ã§ã³ã®ãã³ãã©ãŒãè¿œå ããŸããããã«ã¯ã察å¿ãã` onActionName`ã¹ãã¬ãŒãžã¡ãœããïŒãŸãã¯å¿ èŠã«å¿ã㊠`actionName`ïŒããããŸãã äžèšã®äŸã§ã¯ã `actions`ãªããžã§ã¯ãã«` iceShard`ã¢ã¯ã·ã§ã³ãå«ãŸããŠããå ŽåãïŒå¯Ÿå¿ãããã³ãã©ãŒããªãããïŒåã«ç¡èŠãããŸãã
ãããã㣠`listenables`
ããã«äŸ¿å©ã«ããããã«ãã¢ã¯ã·ã§ã³ãå«ããªããžã§ã¯ãã `listenables`ãªããžããªããããã£ã«å²ãåœãŠããšãèªåçã«` listenToMany`ã«è»¢éãããŸãã ãããã£ãŠãäžèšã®äŸã¯ããã«ç°¡ç¥åã§ããŸãã
var actions = Reflux.createActions(["fireBall","magicMissile"]); var Store = Reflux.createStore({ listenables: actions, onFireBall: function(){ // whoooosh! }, onMagicMissile: function(){ // bzzzzapp! } });
`listenables`ããããã£ã¯ãåæ§ã®ãªããžã§ã¯ãã®é åã«ããããšãã§ããŸãã ãã®å Žåãåãªããžã§ã¯ã㯠`listenToMany`ã«æž¡ãããããã次ã®ããšã䟿å©ã«ãªããŸãã
var Store = Reflux.createStore({ listenables: [require('./darkspells'),require('./lightspells'),{healthChange:require('./healthstore')}], // });
ãªããžããªãžã®ãµãã¹ã¯ã©ã€ãïŒãªããžããªããéä¿¡ãããã€ãã³ãã®åŠçïŒ
ã³ã³ããŒãã³ãã§ã¯ã次ã®ããã«ãªããžããªããã€ãã³ãåŠçããµãã¹ã¯ã©ã€ãã§ããŸãã
// var statusStore = Reflux.createStore({ // init: function() { // statusUpdate this.listenTo(statusUpdate, this.output); }, // output: function(flag) { var status = flag ? 'ONLINE' : 'OFFLINE'; // this.trigger(status); } }); // , function ConsoleComponent() { // statusStore.listen(function(status) { console.log('status: ', status); }); };
var consoleComponent = new ConsoleComponent();
é¢æ°ãšããŠã¢ã¯ã·ã§ã³ãªããžã§ã¯ã `statusUpdate`ã䜿çšããŠãã€ãã³ãããã§ãŒã³ããšã«ãã£ã¹ãããããŸãã
statusUpdate(true); statusUpdate(false);
äžèšã®ããã«ãã¹ãŠãè¡ããšãçµè«ã¯æ¬¡ã®ããã«ãªããŸãã
ã¹ããŒã¿ã¹ïŒãªã³ã©ã€ã³ ã¹ããŒã¿ã¹ïŒãªãã©ã€ã³
Reactã³ã³ããŒãã³ãã®äœ¿çšäŸ
` componentDidMount` [ã©ã€ããµã€ã¯ã«ã¡ãœãã]ïŒïŒã¡ãœããã§Reactã³ã³ããŒãã³ãã®ã¢ã¯ã·ã§ã³ããµãã¹ã¯ã©ã€ããã次ã®ããã«` componentWillUnmount`ã¡ãœããã§ãµãã¹ã¯ã©ã€ã解é€ã§ããŸãã
var Status = React.createClass({ initialize: function() { }, onStatusChange: function(status) { this.setState({ currentStatus: status }); }, componentDidMount: function() { this.unsubscribe = statusStore.listen(this.onStatusChange); }, componentWillUnmount: function() { this.unsubscribe(); }, render: function() { // } });
Reactã³ã³ããŒãã³ãå ã®äŸ¿å©ãªäœæ¥ã®ããã®äžçŽç©
ã³ã³ããŒãã³ãã¯ãé©åãªã¿ã€ãã³ã°ã§ã€ãã³ãããåžžã«ãµãã¹ã¯ã©ã€ã/ãµãã¹ã¯ã©ã€ã解é€ããå¿ èŠãããããã䜿çšã®äŸ¿å®ã®ããã«ã æ··åç© `Reflux.ListenerMixin`ã䜿çšã§ããŸãã ããã䜿çšããŠãäžèšã®äŸã次ã®ããã«æžãæããããšãã§ããŸãã
var Status = React.createClass({ mixins: [Reflux.ListenerMixin], onStatusChange: function(status) { this.setState({ currentStatus: status }); }, componentDidMount: function() { this.listenTo(statusStore, this.onStatusChange); }, render: function() { // render specifics } });
ãã®äžçŽæ§ã«ãããlistenToã¡ãœããã¯ã³ã³ããŒãã³ãå ã§åŒã³åºãããšãã§ããŸããããã¯ãåãååã®storageã¡ãœãããšåãããã«æ©èœããŸãã `listenToMany`ã¡ãœããã䜿çšã§ããŸãã
Reflux.listenToã䜿çšãã
ãcomponentDidMountïŒïŒãå ã§ãthis.listenToïŒïŒãã«é¢ããç¹å®ã®ããžãã¯ã䜿çšããªãå ŽåããReflux.listenToïŒïŒãåŒã³åºããæ··åç©ãšããŠäœ¿çšã§ããŸãã ãã®å Žåã `componentDidMountïŒïŒ`ã¯å¿ èŠã«å¿ããŠèªåçã«èšå®ãããã³ã³ããŒãã³ã㧠`ListenerMixin`ã®æ··åç©ãååŸããŸãã ãããã£ãŠãäžèšã®äŸã次ã®ããã«æžãæããããšãã§ããŸãã
var Status = React.createClass({ mixins: [Reflux.listenTo(statusStore,"onStatusChange")], onStatusChange: function(status) { this.setState({ currentStatus: status }); }, render: function() { // `this.state.currentStatus` } });
åã `mixins`é åå ã«` Reflux.listenTo`ãžã®è€æ°ã®åŒã³åºããæ¿å ¥ã§ããŸãã
åæ§ã«æ©èœãããReflux.listenToManyããããããlistener.listenToManyãã䜿çšã§ããŸãã
Reflux.connectã䜿çšãã
ãªããžããªããããŒã¿ãåä¿¡ãããšãã«ã³ã³ããŒãã³ãã®ç¶æ ãæŽæ°ããã ãã§ããã°ãReactJSã³ã³ããŒãã³ãã®æ··åç©ãšããŠåŒ `Reflux.connectïŒlistenerã[stateKey]ïŒ`ã䜿çšã§ããŸãã ããã§ãªãã·ã§ã³ã®ããŒãstateKeyããæž¡ããšãã³ã³ããŒãã³ãã®ç¶æ ã¯ãthis.setStateïŒ{ïŒdata}ïŒãã䜿çšããŠèªåçã«æŽæ°ãããŸãã ãstateKeyããæž¡ãããªãå Žåããthis.setStateïŒdataïŒããžã®åŒã³åºããè¡ãããŸãã æ°ããæ©èœãåæ ããããã«æžãçŽããäžèšã®äŸã次ã«ç€ºããŸãã
var Status = React.createClass({ mixins: [Reflux.connect(statusStore,"currentStatus")], render: function() { // render using `this.state.currentStatus` } });
Reflux.connectFilterã䜿çšãã
`Reflux.connectFilter`ã¯ã` Reflux.connect`ãšåãããã«äœ¿çšã§ããŸãã ã¹ãã¬ãŒãžç¶æ ã®äžéšã®ã¿ãã³ã³ããŒãã³ãã«è»¢éããå¿ èŠãããå Žåã¯ãäžçŽç©ãšããŠãconnectFilterãã䜿çšããŸãã Refluxã䜿çšããŠæžãããããã°ããã¹ãŠã®åºçç©ããªããžããªã«ä¿æããå¯èœæ§ãé«ããšããŸãããã ãŸããåäžã®æçš¿ã®ããŒãžã§ã `Reflux.connectFilter`ã䜿çšããŠæçš¿ããã£ã«ã¿ãªã³ã°ã§ããŸãã
var PostView = React.createClass({ mixins: [Reflux.connectFilter(postStore,"post", function(posts) { posts.filter(function(post) { post.id === this.props.id; }); })], render: function() { // , `this.state.post` } });
ä»ã®ãªããžããªããã®å€æŽã€ãã³ãã®åŠç
ãªããžããªã¯ãä»ã®ãªããžããªã®å€æŽããµãã¹ã¯ã©ã€ãã§ãããããã¢ããªã±ãŒã·ã§ã³éã®ä»ã®éšåã«åœ±é¿ãäžããããšãªãããªããžããªéã®ããŒã¿è»¢éãã§ãŒã³ãæ§ç¯ããŠããŒã¿ãéçŽã§ããŸãã ãªããžããªã¯ãã¢ã¯ã·ã§ã³ãªããžã§ã¯ãã®å Žåãšåæ§ã«ã `listenTo`ã¡ãœããã䜿çšããŠä»ã®ãªããžããªã§çºçããå€æŽããµãã¹ã¯ã©ã€ãã§ããŸãã
// , , statusStore var statusHistoryStore = Reflux.createStore({ init: function() { // this.listenTo(statusStore, this.output); this.history = []; }, // output: function(statusString) { this.history.push({ date: new Date(), status: statusString }); // this.trigger(this.history); } });
è¿œå æ©èœ
å¥ã®ã€ãã³ã管çã©ã€ãã©ãªã䜿çšãã
ããã©ã«ãã®EventEmitterãæ°ã«å ¥ããªãïŒ æ¬¡ã®ãããªçµã¿èŸŒã¿ããŒããå«ããä»ã®ãã®ã䜿çšããããã«åãæ¿ããããšãã§ããŸãã
// Reflux.setEventEmitter(require('events').EventEmitter);
代æ¿ã®Promiseã©ã€ãã©ãªã䜿çšãã
Promiseã®ããã©ã«ãã®å®è£ ã©ã€ãã©ãªãæ°ã«å ¥ããªãïŒ ä»ã®ããããã®äœ¿çšã«åãæ¿ããããšãã§ããŸãïŒããšãã°ã次ã®ãããªBluebird ïŒ
// Reflux.setPromise(require('bluebird'));
RefluxJSã®ãããã¹ã¯ããnew PromiseïŒ...ïŒããåŒã³åºãããšã§äœæãããããšã«æ³šæããŠãã ããã ã©ã€ãã©ãªããã¡ã¯ããªã䜿çšããå Žåã `Reflux.setPromiseFactoryïŒïŒ`åŒã³åºãã䜿çšããŸãã
Promises Factoryã䜿çšãã
ã»ãšãã©ã®ã©ã€ãã©ãªã¯ãããã¹ãåŠçããããã«ã³ã³ã¹ãã©ã¯ã¿ãŒã䜿çšããªãããïŒ `new PromiseïŒ...ïŒ`ïŒããã¡ã¯ããªãŒãèšå®ããå¿ èŠã¯ãããŸããã
ãã ãã `Q`ã®ãããªãã®ããŸãã¯ãã¡ã¯ããªã¡ãœããã䜿çšããŠãããã¹ãäœæããä»ã®ã©ã€ãã©ãªã䜿çšããå Žåã¯ã` Reflux.setPromiseFactory`åŒã³åºãã䜿çšããŠæå®ããŸãã
// Reflux.setPromiseFactory(require('Q').Promise);
nextTick Alternativeã䜿çšãã
ã¢ã¯ã·ã§ã³ãåŒã³åºããããšããã¡ã³ã¯ã¿ãŒãšããŠåŒã³åºãããéåæã§çºçããŸãã ã³ã³ãããŒã«ã¯ããã«è¿ããã察å¿ãããã³ãã©ãŒãRefluxJSå ã® `setTimeout`ïŒfunction` nextTick`ïŒçµç±ã§åŒã³åºãããŸãã
æé©ãªé 延ã¡ãœããåŒã³åºãïŒ `setTimeout`ã` nextTick`ã `setImmediate`ãªã©ïŒã®å®è£ ãéžæã§ããŸãã
// node.js env Reflux.nextTick(process.nextTick);
ããè¯ã代æ¿ãšããŠã `setImmediate` ãŸã㯠` macrotask` ããªãã£ã«ãå¿ èŠãªå ŽåããããŸã
ãã§ãŒã³å ã®ãã¹ãŠã®ã¢ã¯ã·ã§ã³ãå®äºããã®ãåŸ ã£ãŠããŸã
Reflux APIã«ã¯ãã€ãã³ãã䞊è¡ããŠéä¿¡ãããœãŒã¹ã®äŸ¿å©ãªéçŽãæäŸãã `join`ã¡ãœããããããŸãã ããã¯ãFacebookã®å ã®Fluxå®è£ ã®ãwaitForãã¡ãœãããšåãã§ãã
远跡åŒæ°
ãã¹ãŠã®åå è ãå°ãªããšã1åã€ãã³ããéä¿¡ãããšããã«ã察å¿ãã `joinïŒïŒ`åŒã³åºãã«æž¡ããããã³ãã©ãŒãåŒã³åºãããŸãã ãã³ãã©ãŒã«ã¯ããjoinããåŒã³åºããšãã«æäœã®åå è ã宣èšãããé åºã§åã€ãã³ãã®ãã©ã¡ãŒã¿ãŒãæž¡ãããŸãã
4ã€ã®ãçµåããªãã·ã§ã³ãããããããããç¹å¥ãªããŒã¿æŠç¥ãè¡šããŸãã
- `joinLeading`ïŒæåã®ã€ãã³ãåŒã³åºãã®çµæã®ã¿ãåãããªãã·ã£ãŒããä¿åãããŸãã ä»ã®ãã¹ãŠã®ããŒã¿ã¯ç¡èŠãããŸãã
- joinTrailingïŒæåŸã®ã€ãã³ãåŒã³åºãã®çµæã®ã¿ãåãããªãã·ã£ãŒããä¿åãããŸãã ä»ã®ãã¹ãŠã®ããŒã¿ã¯ç¡èŠãããŸãã
- `joinConcat`ïŒãã¹ãŠã®çµæã¯é åã«ä¿åãããŸãã
- joinStrictïŒåããããªãã·ã£ãŒããã€ãã³ããåŒã³åºããšããšã©ãŒãçºçããŸãã
ãã¹ãŠã®ã¡ãœããã®ã·ã°ããã£ã¯åãããã«èŠããŸãã
joinXyz(...publisher, callback)
joinïŒïŒãå®äºãããšããã«ãããã«é¢é£ä»ããããŠãããã¹ãŠã®å¶éãåé€ããããããªãã·ã£ãŒãåã³ãã§ãŒã³ã«ã€ãã³ããéä¿¡ãããšãåã³æ©èœããããã«ãªããŸãã
ã€ãã³ã管çã«ã€ã³ã¹ã¿ã³ã¹ã¡ãœããã䜿çšãã
ãªã¹ããŒAPIã䜿çšãããã¹ãŠã®ãªããžã§ã¯ãïŒã¹ãã¬ãŒãžãListenerMixinãæ··åããReactã³ã³ããŒãã³ãããŸãã¯ListenerMethodã䜿çšããä»ã®ã³ã³ããŒãã³ãïŒã¯ãäžã§èª¬æããjoinã¡ãœããã®4ã€ã®ãªãã·ã§ã³ã«ã¢ã¯ã»ã¹ããŸãã
var gainHeroBadgeStore = Reflux.createStore({ init: function() { this.joinTrailing(actions.disarmBomb, actions.saveHostage, actions.recoverData, this.triggerAsync); } }); actions.disarmBomb("warehouse"); actions.recoverData("seedyletter"); actions.disarmBomb("docks"); actions.saveHostage("offices",3); // `gainHeroBadgeStore` `[["docks"],["offices",3],["seedyletter"]]`
éçã¡ãœããã䜿çšãã
çµåã¡ãœããã䜿çšããŠãããã§ãŒã³ã«ã€ãã³ããéä¿¡ããããšã¯ãªããžããªã«ãšã£ãŠäžè¬çãªããšãªã®ã§ããã¹ãŠã®çµåã¡ãœããã¯ãæå®ãããã€ãã³ãã«ãµãã¹ã¯ã©ã€ãããããªããžããªãªããžã§ã¯ããè¿ãRefluxãªããžã§ã¯ãã«éçã«çžåœããŸãããããã®æ¹æ³ã䜿çšããŠãäžèšã®äŸã次ã®ããã«æžãæããããšãã§ããŸãã
var gainHeroBadgeStore = Reflux.joinTrailing(actions.disarmBomb, actions.saveHostage, actions.recoverData);
listenToã¡ãœããã䜿çšããŠããã©ã«ãç¶æ ãéä¿¡ãã
ãªããžããªãš `ListenerMixin`ã«ãã£ãŠæäŸããã` listenTo`é¢æ°ã«ã¯3çªç®ã®ãã©ã¡ãŒã¿ãŒããããããã¯é¢æ°ã«ããããšãã§ããŸãããã®é¢æ°ã¯ããã³ãã©ãŒããã©ã¡ãŒã¿ãŒãšã㊠`getInitialState`ãåŒã³åºããçµæã§ç»é²ãããšãã«åŒã³åºãããŸãã
var exampleStore = Reflux.createStore({ init: function() {}, getInitialState: function() { return "- "; } }); // this.listenTo(exampleStore, onChangeCallback, initialCallback) // initialCallback "- "
`listenToMany`ã¡ãœãããèŠããŠããŸããïŒä»ã®ãªããžããªã§äœ¿çšããå Žåã `getInitialState`ããµããŒãããŸãããã®ã¡ãœããã«ãã£ãŠè¿ãããããŒã¿ã¯ãéåžžã®ãã³ãã©ãŒããŸãã¯ååšããå Žå㯠`this.onDefault`ã¡ãœããã«æž¡ãããŸãã
ã³ããã©ã³
- è²¢ç®è ã®ãªã¹ã
- ãããžã§ã¯ãã©ã€ã»ã³ã¹ïŒBSD 3-Clauseã©ã€ã»ã³ã¹ãCopyright 2014ãMikael Brassmanã
- ã©ã€ã»ã³ã¹ã®è©³çŽ°
- Refluxã¯eventemitter3ã䜿çšããŸããããã¯çŸåšMITã§ã©ã€ã»ã³ã¹ãããŠããŸã