Backbone.js 获取和设置嵌套对象属性
我有一个关于 Backbone.js 的 get 和 set 函数的简单问题。
1)使用下面的代码,如何直接“获取”或“设置”obj1.myAttribute1?
另一个问题:
2)在模型中,除了 defaults 对象之外,我可以/应该在哪里声明模型的其他属性,以便可以通过 Backbone 的 get 和 set 方法访问它们?
var MyModel = Backbone.Model.extend({
defaults: {
obj1 : {
"myAttribute1" : false,
"myAttribute2" : true,
}
}
})
var MyView = Backbone.View.extend({
myFunc: function(){
console.log(this.model.get("obj1"));
//returns the obj1 object
//but how do I get obj1.myAttribute1 directly so that it returns false?
}
});
我知道我可以这样做:
this.model.get("obj1").myAttribute1;
但是这是好的做法吗?
I have a simple question about Backbone.js' get and set functions.
1) With the code below, how can I 'get' or 'set' obj1.myAttribute1 directly?
Another question:
2) In the Model, aside from the defaults object, where can/should I declare my model's other attributes, such that they can be accessed via Backbone's get and set methods?
var MyModel = Backbone.Model.extend({
defaults: {
obj1 : {
"myAttribute1" : false,
"myAttribute2" : true,
}
}
})
var MyView = Backbone.View.extend({
myFunc: function(){
console.log(this.model.get("obj1"));
//returns the obj1 object
//but how do I get obj1.myAttribute1 directly so that it returns false?
}
});
I know I can do:
this.model.get("obj1").myAttribute1;
but is that good practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
虽然 this.model.get("obj1").myAttribute1 很好,但有点问题,因为这样你可能会想对 set 做同样类型的事情,即
但是如果你这样做,您将无法获得
myAttribute1
的 Backbone 模型的优势,例如更改事件或验证。更好的解决方案是永远不要在模型中嵌套 POJSO(“普通的旧 JavaScript 对象”),而是嵌套自定义模型类。所以它看起来像这样:
然后访问代码将是
,但更重要的是设置代码
将触发适当的更改事件等。此处的工作示例: http://jsfiddle.net/g3U7j/
While
this.model.get("obj1").myAttribute1
is fine, it's a bit problematic because then you might be tempted to do the same type of thing for set, i.e.But if you do this, you won't get the benefits of Backbone models for
myAttribute1
, like change events or validation.A better solution would be to never nest POJSOs ("plain old JavaScript objects") in your models, and instead nest custom model classes. So it would look something like this:
Then the accessing code would be
but more importantly the setting code would be
which will fire appropriate change events and the like. Working example here: http://jsfiddle.net/g3U7j/
我为此创建了 backbone-deep-model - 只需扩展 Backbone.DeepModel 而不是 Backbone。模型,然后您可以使用路径来获取/设置嵌套模型属性。它也维护变更事件。
I created backbone-deep-model for this - just extend Backbone.DeepModel instead of Backbone.Model and you can then use paths to get/set nested model attributes. It maintains change events too.
Domenic 的解决方案将起作用,但是每个新的 MyModel 将指向 Obj 的同一个实例。
为了避免这种情况,MyModel 应该如下所示:
请参阅 c3rin 的答案 @ https://stackoverflow.com/a/6364480/1072653以获得完整的解释。
Domenic's solution will work however each new MyModel will point to the same instance of Obj.
To avoid this, MyModel should look like:
See c3rin's answer @ https://stackoverflow.com/a/6364480/1072653 for a full explanation.
我使用这种方法。
如果您有这样的 Backbone 模型:
您可以设置属性“ab”:
现在您的模型将具有如下属性:
触发“change”事件。
I use this approach.
If you have a Backbone model like this:
You can set the attribute "a.b" with:
Now your model will have attributes like:
with the "change" event fired.
有一种还没有人想到的解决方案,而且可以大量使用。您确实无法直接设置嵌套属性,除非您使用您可能不想要的第三方库。但是,您可以做的是克隆原始字典,在那里设置嵌套属性,然后设置整个字典。小菜一碟。
There is one solution nobody thought of yet which is lots to use. You indeed can't set nested attributes directly, unless you use a third party library which you probably don't want. However what you can do is make a clone of the original dictionary, set the nested property there and than set that whole dictionary. Piece of cake.
我对 @Domenic 的解决方案遇到了 @pagewil 和 @Benno 相同的问题。我的答案是编写一个简单的 Backbone.Model 子类来解决问题。
NestedModel 为您做的就是允许这些工作(这就是通过 JSON 数据设置 myModel 时发生的情况):
在初始化时自动生成模型列表很容易,但这个解决方案对我来说已经足够好了。
I had the same problem @pagewil and @Benno had with @Domenic's solution. My answer was to instead write a simple sub-class of Backbone.Model that fixes the problem.
What NestedModel does for you is allow these to work (which is what happens when myModel gets set via JSON data):
It would be easy to generate the models list automatically in initialize, but this solution was good enough for me.
Domenic 提出的解决方案有一些缺点。假设您想收听“更改”事件。在这种情况下,不会触发“initialize”方法,并且您的自定义属性值将替换为来自服务器的 json 对象。在我的项目中我遇到了这个问题。我覆盖模型“set”方法的解决方案:
Solution proposed by Domenic has some drawbacks. Say you want to listen to 'change' event. In that case 'initialize' method will not be fired and your custom value for attribute will be replaced with json object from server. In my project I faced with this problem. My solution to override 'set' method of Model:
虽然在某些情况下使用 Backbone 模型而不是嵌套对象属性是有意义的,如 Domenic 提到的,但在更简单的情况下,您可以在模型中创建一个 setter 函数:
While in some cases using Backbone models instead of nested Object attributes makes sense as Domenic mentioned, in simpler cases you could create a setter function in the model:
如果与后端交互,则需要具有嵌套结构的对象。
但有了主干,更容易与线性结构一起工作。
backbone.linear 可以帮助你。
If you interact with backend, which requires object with nesting structure.
But with backbone more easy to work with linear structure.
backbone.linear can help you.