Make Backbone.js Changing the Model “Partially” Silent?

When "chartModel" changes me, I want to update "globalModel".

chartModel.bind("change", updateGlobalModel); updateGlobalModel(){ globalModel.set(obj) } 

Conversely, I want my chartModel to update when the globalModel changes.

 globalModel.bind("change", updateChartModel); updateChartModel(){ chartModel.set(obj) } 

This leads to a feedback loop when setting up globalModel. I could prevent this by setting {silent: true}.

But there is a problem. I have another model that depends on the change event:

 globalModel.bind("change", updateOtherModel); 

How can I warn this model of change, but not the same (to avoid a feedback loop)?

UPDATE:
At the moment, I decided to create a specific identifier for each given call:

 set : function(attrs, options) { if(!("setID" in attrs)){ attrs.setID = myApp.utils.uniqueID(); //newDate.getTime(); } Backbone.Model.prototype.set.call(this, attrs, options); }, 

That way, I can always generate the "setID" attribute from anywhere in the application. If the setID remains unchanged while retrieving anything from the model, I know that there may be a risk for the feedback loop.

+6
source share
2 answers

My knowledge is limited, so maybe I should not answer, but I will try to transfer the link to chartModel when it is created, and refers to the "other" model that you want to update. Then fire the updateChartModel () event and make sure your “other” model is associated with this event.

I have a question: does the silent disconnect all events? Or just related to the model? Obviously, this will not work if all events are disabled.

0
source

Better late than never.

The easiest way to do this is to use a flag. For example, when setting up something in globalModel you can also change the model property to indicate that you have changed something. Then you can check the value of this flag in updateChartModel . For instance:

chartModel.bind ("change", updateGlobalModel);

 function updateGlobalModel() { if (!flag) { globalModel.set(obj); flag = true; } } 

It is likely very likely that you ended up with your setID. Aside, underscore has a uniqueId built-in function.

Another thing you can do that is much cleaner is to pass an option with your call sets.

 chartModel.set(obj, { notify : false }); 

Yes, you can pass any parameters you want, you are not limited only to { silent : true } . See this github discussion for more details. Then you check for this property, where you handle the change events as follows:

 function updateGlobalModel(model, options){ // explicitly check for false since it will otherwise be undefined and falsy // you could reverse it.. but I find this simpler if (options.notify !== false) { globalModel.set(obj) } } 

and in the third (and other models) you can simply refuse this check.

The last option is, of course, to look at your design. If these two models are so closely related to each other that they need to be synchronized with each other, it may make sense to combine their functionality. In addition, you can share common functionality. It all depends on your specific situation.

+1
source

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


All Articles