【发布时间】:2012-05-12 00:43:41
【问题描述】:
我是主干 js 的新手,我的第一次交互似乎很完整。问题是这样的
我做了一个这样的模型
window.Dir = Backbone.Model.extend({
defaults: {
"Name": "",
"ChildNodes": new window.FSNodeCollection() },
GetFullName: function () { this.get("id"); }
});
window.FSNodeCollection 是我制作的其他一些集合。
你会注意到我已经将 ChildNodes 属性的值设置为new window.FSNodeCollection()
根据我所阅读的内容,这应该将 ChildNodes 的值设置为新的 window.FSNodeCollection()
但是当我像使用这种模型时
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 的 ChildNodes 中
我做错了什么?
当我尝试做这样的事情时
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)
问题不再存在。
【问题讨论】:
标签: model-view-controller backbone.js