
Question:
Is there a way to create an observable array or in memory collection in Meteor?
The way I'm faking it is by creating a session variable containing the array, Session.setDefault('people', [])
; and then updating that value when the array changes, Session.set('people', modifiedArray)
.
You can create a local collection by calling Meteor.Collection
constructor without supplying collection name in the parameter, i.e.:
LocalList = new Meteor.Collection();
See this in <a href="http://docs.meteor.com/#meteor_collection" rel="nofollow">the Meteor documentation</a>.
Notice also that you can observe anything you want thanks to <a href="http://docs.meteor.com/#deps_dependency" rel="nofollow">Dependencies</a>.
Example:
List = function() {
this.data = [];
this.dep = new Deps.Dependency();
};
_.extends(List.prototype, {
insert: function(element) {
this.data.push(element);
this.dep.changed();
},
});
var list = new List();
Template.observer.helper = function() {
list.dep.depend();
return list.data;
};
helper
will be updated and observer
template will rerender each time you call list.insert
function.