簡體   English   中英

Backbone.js添加到集合

[英]Backbone.js adding to Collection

在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可能有問題,並且您的文檔尚未准備好嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM