在 Backbone 中,我有一个集合,其中填充了一些 JSON 数据,如下所示。
[
{
"test1": {
"fistName": "test",
"lastName": "example"
},
"test2": {
"fistName": "test",
"lastName": "example"
}
},
{
"test1": {
"fistName": "test",
"fistName": "example"
},
"test2": {
"fistName": "test",
"fistName": "example"
}
},
]
目前我正在尝试向包含上述数据的集合添加一个新模型。
这是模型。
Test = Backbone.Model.extend({
defaults : {
test1: {
firstName: null,
lastName: null
},
test2: {
firstName: null,
lastName: null
}
},
});
下面是我正在尝试的
var test = new Test({test1: {firstName: $("#test1 option:selected").val(), score: $("#test1lastName").val()}}, {test2: {firstName: $("#test2 option:selected").val(), score: $("#test2lastName").val()}});
myCollection.add(test);
但是,这样做只会填充 test1 数据,而不填充 test2 数据。将 test1 和 test2 数据添加到模型中的正确方法是什么,然后可以将其添加到集合中。
谢谢
更新
澄清一下,测试 1 和 2 不是独立的对象,它们彼此相关并且需要位于同一模型中
最佳答案
根据模型的定义方式进行编辑,如果按如下格式对其进行格式化,则可能可以更好地调试。
var test = new TFS.Test({
test1: {
firstName: $("#test1 option:selected").val(),,
lastName: '', // code for last name?
score: $("#test1lastName").val()
},
test2: {
firstName: $("#test2 option:selected").val(),
lastName: '',
score: $("#test2lastName").val()
}
});
myCollection.add(test);
如果您更好地了解整个操作/流程,我也许可以为您提供更多帮助 - 即是什么触发了这些模型的创建? jQuery 是否有问题,并且您的文档尚未准备好?
关于javascript - Backbone.js 添加到集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11539362/