Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Backbone.js添加到集合中_Javascript_Json_Backbone.js - Fatal编程技术网

Javascript Backbone.js添加到集合中

Javascript Backbone.js添加到集合中,javascript,json,backbone.js,Javascript,Json,Backbone.js,在主干网中,我有一个集合,其中填充了一些JSON数据,如下所示 [ { "test1": { "fistName": "test", "lastName": "example" }, "test2": { "fistName": "test", "lastName": "example" } }, { "test1": { "fistName": "test",

在主干网中,我有一个集合,其中填充了一些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可能有问题,并且您的文档没有准备好

编辑,这取决于您的模型是如何定义的,如果您将其格式化为下面的格式,您可能能够更好地进行调试

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可能有问题,并且您的文档没有准备好

谢谢你的回复,不,我只是想添加一个对象,作为test1和test2 a,它们彼此相关,需要在同一个模型中。模型没有任何问题,你创建新模型的代码在哪里,你的html看起来是什么样的…?谢谢你的回复,不,我只是想添加一个对象,作为相互关联的test1和test2 a,并且需要在同一个模型中。模型没有什么问题,创建新模型的代码在哪里,html是什么样子的。。。。?