javascript - Backbone.js - multiple models for one view -
i have template needs receive data 2 different api endpoints (urls): cart
, user
.
i want 2 endpoints act 1 model or collection can .changedattributes()
or sync
, or fetch
.
i know backbone permissive, lost.
playground:
i've created codepen see i've done far: http://codepen.io/anything/pen/axoboa
desired result should like:
initialize: function(){ var self = this; collection.fetch({ success: function(data){ self.collection = data; } }) }, render: function(){ var self = this; var source = $("#template").html(); var template = handlebars.compile(source); var htmltorender = template(self.collection.tojson()); }
you create event concentrator listening registered objects , retrigger events catch.
something like
var aggregate = _.extend({}, backbone.events); aggregate.register = function(m) { var self = this; this.listento(m, 'all', function() { this.trigger.apply(this, arguments); }); };
you use this
aggregate.on('change', function(m) { // have when 1 of models change console.log('change on ', m.tojson()); }); aggregate.on('sync', function(m) { //same thing syncs console.log('sync ', m.tojson()); }); var m1 = new backbone.model({id: 1}); var m2 = new backbone.model({id: 2}); aggregate.register(m1); aggregate.register(m2); m1.fetch(); m2.set({data: 2});
Comments
Post a Comment