Backbone.js的访问模式模型中的属性 - this.attribute VS this.get('属性')? [英] backbone.js accessing model attributes within model - this.attribute VS this.get('attribute')?

查看:164
本文介绍了Backbone.js的访问模式模型中的属性 - this.attribute VS this.get('属性')?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的理解应该Backbone.js的一个模型的属性,说被宣布为有些私有成员变量

From my understanding the attributes of a Backbone.js model are supposed to be declared as somewhat private member variables by saying

this.set({ attributeName: attributeValue })
// accessing the value
this.get('attributeName');

但是,当我写功能whitin实际的模型似​​乎更简单地说是这样的:

But when I am writing functions whitin the actual model it seems much simpler to say like this:

this.attributeName = attributeValue;
// accessing the value
this.attributeName;

此外,我会假设,后一个版本将是更快的来处理,因为它没有经过Backbone.js的的事件管理。

Also I would assume that the latter version would be faster to process since it doesn't go through backbone.js's event management.

所以我想知道你怎么利弊与主要在模型内部使用的属性做。这些都是人会真正想有点从外面所以让他们暴露喜欢在后一个例子屏蔽可能是不正确的依然是属性。我一直在寻找的Backbone.js的例子查看它没有get和set方法似乎细做像在第二个例子。那么,有经验的好的规则,当模型中编码时使用的get / set(属性)或this.attribute?或者一个模型,使的例子更清楚?

So I was wondering how you pros do with attributes that are primarily used internally in the model. These are the attributes that one would actually want to be a bit shielded from the outside so having them exposed like in the latter example maybe isn't right still. When I have been looking at examples for the backbone.js view which doesn't have get and set methods it seems fine to do like in the second example. So is there any nice rule of thumb when to use get/set(attribute) or this.attribute when coding within the model? Or maybe an example of a model that makes this clearer?

推荐答案

何时使用 model.get(财产)模型.SET(...)

When to use model.get(property) and model.set(...)

您应该使用 GET 设置来访问模型的数据的。这意味着,有一个使用检索模型的序列重新presentation的一部分的任何属性,并坚持使用保存

You should use get and set to access the model's data. This means any attributes that are part of the model's serialized representation that is retrieved using fetch and persisted using save.

何时使用 model.attributes.property

When to use model.attributes.property

从不。

您应该总是使用 GET 键,尤其是,设置,而不是通过访问模型.attributes 对象直接,虽然我已经看到了这个意见冲突。我相信有一个模型之间的合约和它的消费者,这保证了消费者可以任意改变模型的数据使用变更通知事件。如果修改了内部属性直接对象,事件不会发送这种合同被打破了。骨干事件的非常的快,特别是如果你不重视他们的监听器,它不是一个点,从你的一部分过度优化的好处。

You should always use get, and especially set, instead of accessing the model.attributes object directly, although I've seen conflicting opinions about this. I believe there is a contract between a model and it's consumers, which guarantees that the consumer can be notified of any changes to the model's data using the change event. If you modify the internal attributes object directly, events are not sent and this contract is broken. Backbone events are very fast, especially if you don't have any listeners attached to them, and it's not a point that benefits from over-optimization on your part.

虽然访问属性,而不是直接的 GET 是它自己很无害的,应该避免这样的属性对象可以被认为是完全,完全私密。

Although accessing the attributes directly instead of get is quite harmless on it's own, it should be avoided so the attributes object can be considered totally, completely private.

如果你绝对需要prevent一些变化触发事件,则可以使用沉默:真正的选项: model.set({键:VAL},{沉默:真})。这确实打破了上述合同,甚至骨干自己的文档提供了以下警告:

If you absolutely need to prevent some change triggering events, you can use the silent:true option: model.set({key:val}, {silent:true}). This does break the aforementioned contract, and even Backbone's own documentation gives the following caveat:

请注意,这是很少,甚至永远,是一个好主意。通过这些选项的具体标志为您的活动回调来看待,并选择忽略,通常会制定出更好的通过。

Note that this is rarely, perhaps even never, a good idea. Passing through a specific flag in the options for your event callback to look at, and choose to ignore, will usually work out better.

何时使用 model.property

When to use model.property

它们的不限属性不是数据的,即,临时状态变量,计算出特性等可以直接连接到模型实体。这些性质应被视为临时性的,传递的:他们可以在模型初始化或在其一生中被重建,但他们不应该被持久化,公共或私人。一个典型的命名约定是与 preFIX私有财产_ 字符如下:

Any properties which are not data, i.e. temporary state variables, calculated properties etc. can be attached directly to the model entity. These properties should be considered temporary and transitive: they can be recreated upon model initialization or during its lifetime, but they should not be persisted, whether public or private. A typical naming convention is to prefix private properties with the _ character as follows:

this._privateProperty = 'foo';
this.publicProperty = 'bar';

这篇关于Backbone.js的访问模式模型中的属性 - this.attribute VS this.get('属性')?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆