Override the baseline "set" method

I want to override the base set method so that whenever I set a value for the base model, callbacks registered in this attribute are called without checking the same previous value of this attribute.

var model = Backbone.Model.extend({ defaults : { prop1 : true } }); var view = Backbone.View.extend({ initialize : function(){ this.listenTo(this.model,"change:prop1", this.callback); }, callback : function(){ // set is called on prop1 } }); var m1 = new model(); var v1 = new view({model:m1}); m1.set("prop1",true); // It doesn't trigger callback because I'm setting the same value to prop1 
+6
source share
1 answer

You can write a new method in the base model, for example:

 var model = Backbone.Model.extend({ defaults: { prop1: true; }, // Overriding set set: function(attributes, options) { // Will be triggered whenever set is called if (attributes.hasOwnProperty(prop1)) { this.trigger('change:prop1'); } return Backbone.Model.prototype.set.call(this, attributes, options); } }); 
+19
source

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


All Articles