In my Backbone.js project I have one Model and several Views. All Views have registered callbacks for 'change:currentTextTitle'
on this model:
// 'this' stands for any of the Views here
myModel.on('change:currentTextTitle', this.render, this);
Now a user performs some action, which causes the specific View to change its "current text title" field. This specific view then calls myModel.set("currentTextField", newTextValue)
which in turn triggers the 'change:currentTextTitle'
event calling all Views (including the one from which set() originated). Then all Views call their render
callback functions.
The problem is that the render
method is also called on the View from which the set()-Method was originally called, which is completely unnecessary because it is already up-to-date with currentTextTitle
.
How would my Views call myModel.set() in a way that the other Views' callbacks get informed, but without triggering/calling the "source View" itself?
One workaround seems to be to pass the source view as part of the options
parameter of the set()
method (which gets passed along to trigger()
and then along the the render()
callback):
myModel.set("currentTextField", newTextValue, thisViewSetAttribute)
Then in the render
callback one could check if thisViewSetAttribute != this
. However, instead of implementing checks in every callback, I think it would make more sense to handle this in the Model itself by only calling the necessary callbacks and ignoring the source View from which the set() method call originated. Is this possible?