backbone.js - backbone collection change doesn't get triggered when changing model -
backbone.js - backbone collection change doesn't get triggered when changing model -
i'm building frontend backbone on top of api , wonder, why change
event doesn't triggered. fetching , saving works, template doesn't rerendered.
collection
var clients = backbone.collection.extend({ url: api.host + '/clients', model: client });
model
var client = backbone.model.extend({ urlroot: api.host + '/clients', defaults: { clients: { number: '', company: '', address1: '', address2: '', city: '', zip: '', country: '', tax: '', email: '', phone: '' } } });
view
var clientsview = backbone.view.extend({ id: 'content', initialize: function() { // instantiate collection this.model = new clients(); // compile handlebars template this.tpl = handlebars.compile(this.template); // bind alter , reset model events rerender template this.model.bind('change', this.render); this.model.bind('reset', this.render); }, render: function() { var self = this; var obj = this.el; // clients , set info handlebars template $.when(this.model.fetch()).then(function(response) { $(obj).html(self.tpl(response)); }, function() { $(obj).html(self.tpl); }); homecoming this; }, events: { "click #client-save": "save" }, save: function(e) { // cache target , parent object quick form access var obj = e.target; var parent = $(obj).closest('#client-modal'); var client = new client({ clients: { number: parent.find('[name="number"]').val(), company: parent.find('[name="company"]').val(), address1: parent.find('[name="address1"]').val(), address2: parent.find('[name="address2"]').val(), city: parent.find('[name="city"]').val(), zip: parent.find('[name="zip"]').val(), country: parent.find('[name="country"]').val(), tax: parent.find('[name="tax"]').val(), email: parent.find('[name="email"]').val(), phone: parent.find('[name="phone"]').val(), web: parent.find('[name="web"]').val() } }); client.save(); homecoming false; } });
you need pass context when bind functions.
this.model.bind('change', this.render, this)
try utilize name collection instance.
// illustration this.collectionclients = new clients();
\0/
backbone.js
Comments
Post a Comment