javascript - Meteor Deps.autorun doesn't get triggerd - what's its use compared to observe()? -
i have test collection ("initdata") contains documents , following code on meteor client:
meteor.subscribe("initdata"); meteor.startup(function () { console.log(initdata.findone("randidtest").key); deps.autorun(function() { var allinitdata = initdata.find(); allinitdata.foreach(function(entry) { console.log("deps foreach: " + entry._id); }); var randomobject = initdata.findone("randidtest"); console.log("deps call " + randomobject.key); }); }); var initdataobserver = initdata.find().observe({ added: initdatachange, changed: initdatachange, removed: initdatachange }); function initdatachange () { var allinitdata = initdata.find(); allinitdata.foreach(function(entry) { console.log("observer foreach: " + entry._id); }); var randomobject = initdata.findone("randidtest"); console.log("observer call " + randomobject.key); }
my initial goal find best way use data collection loaded in client, not case when meteor.startup triggered. (in code above on line 4 error initdata.findone("randidtest") undefined @ moment.)
i tried accomplish deps.autorun() , observe() can see above. observe() works expect. deps.autorun() doesn't triggered @ (no matter if delete, modify or add documents - , i've tried put outside meteor.startup(fucntion(){})).
am understanding deps.autorun() wrong or have implemented wrong? in case should use deps.autorun() , when observe()?
firstly, docs:
if initial run of autorun throws exception, computation automatically stopped , won't rerun.
so if meteor.startup
block executing before subscription initdata
ready , randomobject
undefined, appears case given error thrown, deps.autorun
won't run again regardless of do.
there methods in iron-router package deal situation easily, , recommend explore them. in above example it's quite straightforward avoid error being thrown on first run checking whether randomobject
defined, should deps.autorun
work expected. either or observe
block seem trick use case describe both triggered when minimongo db populated on client, iron-router solution may cleaner.
Comments
Post a Comment