json - Meteor `Deps.autorun` vs `Collection.observe` -
what pros/cons between using deps.autorun
or collection.observe
keep third-party widget in sync reactive meteor.collection
.
for example, using jstree visually show directory tree have stored in mongodb. i'm using code make reactive:
// automatically reload filetree if data changes filetree.find().observechanges({ added: function() { $.jstree.reference('#filetree').refresh(); }, changed: function() { $.jstree.reference('#filetree').refresh(); }, removed: function() { $.jstree.reference('#filetree').refresh(); } });
what pros/cons of using method vs deps.autorun
call this: (untested)
deps.autorun(function() { jsondata = filetree.find().fetch(); $.jstree.reference('#filetree')({'core': {'data': jsondata} }); });
this example. i'm asking pros/cons in general, not specific use case.
deps.autorun, tracker.autorun reactive computation block. whereas observechanges provides callback when changes.
when use deps.autorun, entire block in function() {...}
, re-run every time reactive variable, or document changes, in way @ (that updated, removed or inserted), or other reactive variable change.
the observechanges callbacks more fine tuned, , fire callbacks added, changed or removed depending on query.
based on code above, in effect both same. if had more reactive variables in deps.autorun block observechanges
way of doing more efficient.
in general first style more efficient, code stands above they're both same , depends on preference.
Comments
Post a Comment