问题描述
即时通讯新的骨干JS和我的第一个互动似乎pretty完井。问题是这样的结果
我已经做了模型像这样
im new to backbone js and my first interaction seems pretty comple. the problem goes like this
i have made a model like this
window.Dir = Backbone.Model.extend({
defaults: {
"Name": "",
"ChildNodes": new window.FSNodeCollection() },
GetFullName: function () { this.get("id"); }
});
window.FSNodeCollection
的是,我已经取得了一些其他集合。
因为你会发现我已经把子节点的属性的值新window.FSNodeCollection()
搜索结果
这个据我已阅读应子节点
的值设置为一个新的 window.FSNodeCollection()
结果
但是,当使用这种模式像IM
window.FSNodeCollection
is some other collection that i have made.as you will notice i have put the value of ChildNodes attribute to new window.FSNodeCollection()
this according to what i have read should set the value of ChildNodes
to a new window.FSNodeCollection()
but when im using this model like
var fsNode = new window.FSNode({
Type: "dir",
Node: new window.Dir({
Name: "dir1",
})
});
var fsNode2 = new window.FSNode({
Type: "dir",
Node: new window.Dir({
Name: "dir2",
})
});
var subDir1Node = new window.FSNode({
Type: "dir",
Node: new window.Dir({
Name: "subDir1",
ChildNodes:new window.FSNodeCollection()
})
});
fsNode.get("Node").get("ChildNodes").push(subDir1Node);
的 subDir1Node
被添加到子节点
两者 fsNode
和 fsNode2
结果
我究竟做错了什么?搜索结果
当我尝试做这样的事情。
the subDir1Node
is being added to the ChildNodes
of both fsNode
and fsNode2
what am i doing wrong?
when i try doing something like this
var fsNode = new window.FSNode({
Type: "dir",
Node: new window.Dir({
Name: "dir1",
ChildNodes:new window.FSNodeCollection()
})
});
var fsNode2 = new window.FSNode({
Type: "dir",
Node: new window.Dir({
Name: "dir2",
ChildNodes:new window.FSNodeCollection()
})
});
var subDir1Node = new window.FSNode({
Type: "dir",
Node: new window.Dir({
Name: "subDir1",
ChildNodes:new window.FSNodeCollection()
})
});
fsNode.get("Node").get("ChildNodes").push(subDir1Node)
问题依然没有更多的。
the problem remains no more.
推荐答案
的问题是,它们都引用同一个数组,你需要做的是确保当您创建模型,你在这一点上初始化数组(你可以使用初始化方法也可以分配将分配集合函数)。
The problem is that they are both referencing the same array, what you need to do is make sure that when you create your model you initialize your array at that point (you can use the initialize method or you can assign a function that will assign the collection).
您可能想看一看这些问题
You might want to take a look at these questions
Arrays在Backbone.js的型号基本上都是静态的?
<一个href=\"http://stackoverflow.com/questions/9970490/why-do-new-instances-of-a-backbone-js-model-contain-additional-values-to-the-$p$p/\">Why一个Backbone.js的模式做新的实例包含附加价值的predefined默认?
这篇关于在&QUOT提供新的集合;默认&QUOT; Backbone.js的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!