Truly without the slightest change in the model attribute in Backbone.js?

Is it possible to change attributes on a model without triggering a change event? If you pass {"silent":true} right now, then the next time the attribute is changed, the event will be triggered without changes. Can I change an attribute safely without a change event ever triggering?

from change, Backbone 0.9.2:

 // Silent changes become pending changes. for (var attr in this._silent) this._pending[attr] = true; // Silent changes are triggered. var changes = _.extend({}, options.changes, this._silent); this._silent = {}; for (var attr in changes) { this.trigger('change:' + attr, this, this.get(attr), options); 
+4
source share
3 answers

I think the cleanest way, if you really want to disable by default (but still be able to do silent: false), would override set . This should do it:

 var SilentModel = Backbone.Model.extend({ set: function(attrs, options) { options = options || {}; if (!('silent' in options)) { options.silent = true; } return Backbone.Model.prototype.set.call(this, attrs, options); } }); 
+5
source

You can directly modify model attributes using model.attributes['xyz'] = 123 .

+11
source
 item.set( { sum: sum ,income: income }, {silent: true} ); 

since trunk 0.9.10

0
source

Source: https://habr.com/ru/post/1402955/


All Articles