backbone.js - Backbone - Using fetched data inside the view -
i have fatched data collection variable, not sure should next. how use data , fill template it? here code render function inside view.
collection.url = "../data"; collection.fetch(); var compiled = _.template(self.data); self.$el.prepend(compiled(/*my json should go here*/));
i newbie backbone, every appreaciated.
here collection definition:
var maincollection = backbone.collection.extend({ model: mainmodel, //localstorage: new backbone.localstorage("kitchen"), initialize: function (models,options) { } }), collection = new maincollection;
here log of collection , collection coverted json:
assuming collection collection's name (that's pretty confusing have say), this you're looking for:
self.$el.prepend(compiled(collection.tojson()));
edit:
don't forget you're fetching data asynchronously. when you're evaluating template, data hasn't come yet, , collection's still empty. listen end of request ('sync' event think) or other events know when collection's populated or use success option of fetch method specify callback :)
as logs. when log object, automatically updated until check details. logged while empty, checked after populated (a few ms afterwards).
Comments
Post a Comment