i'm trying learn how reflux actions , stores work wrote small test program. when run application, code calls
togglegem(); (which causes reflux action fire) not run. these action events queued can shown output of program below.
the output is:
myobj=lastupdate=undefined myobj=lastupdate=undefined myobj=lastupdate=undefined enter: gemstore.handletogglegem: isgemactivated:false myobj.ongemchange: newval=true exit: gemstore.handletogglegem: isgemactivated:true enter: gemstore.handletogglegem: isgemactivated:true myobj.ongemchange: newval=false exit: gemstore.handletogglegem: isgemactivated:false notice console output
enter: gemstore.handletogglegem: isgemactivated:false doesn't appear until program exiting. (to me) means action events bundled , fired/run later "housekeeping" reflux doing.
is there function can call tell reflux fire action events have been queued?
to recreate example, save code refluxtest.js , run these commands:
mkdir refluxtest npm init npm install --save reflux npm install --save react node refluxtest.js thanks.
note: know not representative of how reflux used because outside of browser curious if had answer.
based on code http://spoike.ghost.io/deconstructing-reactjss-flux/
// code file refluxtest.js // kws: learning how reflux works outside of browser var react = require("react/addons"); var testutils = react.addons.testutils; var reflux = require("reflux"); // creating action var togglegem = reflux.createaction(); // creates datastore var gemstore = reflux.createstore({ // initial setup init: function() { this.isgemactivated = false; // register statusupdate action this.listento(togglegem, this.handletogglegem); }, // callback handletogglegem: function() { console.log('enter: gemstore.handletogglegem: isgemactivated:' + this.isgemactivated); this.isgemactivated = !this.isgemactivated; // pass on listeners through // datastore.trigger function this.trigger(this.isgemactivated); console.log('exit: gemstore.handletogglegem: isgemactivated:' + this.isgemactivated); } }); function myobj() { gemstore.listen(this.ongemchange); this.lastupdate = undefined; } myobj.prototype.tostring = function() { return "lastupdate=" + this.lastupdate; } myobj.prototype.ongemchange = function(newval){ console.log("myobj.ongemchange: newval=" + newval); this.lastupdate = newval; } var myobj = new myobj(); console.log("myobj=" + myobj); togglegem(); console.log("myobj=" + myobj); togglegem(); console.log("myobj=" + myobj);
i began debug , step reflux code , first statement checks see if "sync" flag set, tried setting it, , worked!
so changing code add togglegem.sync = true shown below
// creating action var togglegem = reflux.createaction(); togglegem.sync = true; // line added the sample program works expected.
note: after sync problem asked fixed, noticed bug/problem related javascript "this". below additional change needed:
function myobj() { gemstore.listen(this.ongemchange, this); this.lastupdate = undefined; } if this not passed in gemstore.listen call, code not work properly.
Comments
Post a Comment