backbone.js accessing model attributes within model - this.attribute VS this.get('attribute')? -
I think a backbone.GS model is declared as a special member variable by saying >
this.set ({attributeName: attributeValue}) // this value access.get ('attributeName'); But when I distract the real model, it sounds pretty easy to say:
this.attributeName = attributeValue; Using // value.attributeName; In addition, I'll assume that the later version will be faster for the process because it does not go through the event management of backbone.js.
So I was wondering how you do with attributes that are primarily used internally in the model. These are the qualities that one really wants to be shielded from outside so that it can be highlighted as an example of the latter, is still not correct when I look at examples for the backbone.js view which can be obtained in another example And if it does not look like, is there a good rule of thumb when using get / set (attribute) or this.attribute while coding within the model? Or perhaps an example of a model that makes it clear?
to use Model.get (property) and model.set (...) get you the get and data Code for> Set . This means that any property that is part of the random representation of the model is used by using fetch and save . Never model.attributes.property
You should always get , and especially use set , model.attributes to access the item directly. Instead, though I have seen contradictory opinions about this. I believe that there is a contract between a model and it is a consumer, which guarantees that the user will have to change the change can be informed about any changes in the model's data using the event if If you directly modify the internal characteristics, the event has happened are not sent and broken contract. The events of the backbone Too many are fast, especially if you do not have any listeners attached, and this is not a point that you gain from over-optimization on your part. This particular feature is harmless on its own instead of found , it should be avoided so that the property object should be completely considered completely private. Could. If you need to stop the event that triggers some changes, you can silent: correct option: model.set ({key: val}, {silent : True}) . It breaks the above contract, and even the backbone's own documentation gives the following warning: Note that this is probably hardly ever, never, a good idea is.
To use model.property to use, you must choose to pass and ignore the options of your event callback through a specific flag. Any property which is not data , i.e. floating state variable, calculation properties etc. Can be connected directly to the model unit. These properties should be considered temporary and dynamic: they can be rebuilt during the model launch or its lifetime, but they should not be made public or private. The prefix of the personal property with the character _ is as follows: this._privateProperty = 'foo'; This.publicProperty = 'Bar';
Comments
Post a Comment