簡體   English   中英

Backbone.js-有關使用Rails和嵌套集合的指南

[英]Backbone.js - guidance on working with rails and nested collections

我在一個嵌套的模型示例中使用codebrew \\ backbone-rails(例如,我有一個Tasks集合,每個Tasks都有一個Details集合-與示例類似。)

我可以加載並創建一系列嵌套視圖以顯示所需的數據,但是現在在嘗試對該數據執行CRUD操作時,我陷入了困境。

例如,假設我更改了外部(上層)對象上的屬性,並希望將該數據發送到服務器。 這就是json的樣子。 由於我在加載應用程序時“急切地”加載了嵌套數據,因此我將通過更新將其發送回服務器(請查看details_attributes格式)

{
    "task" => {
                      "name" => "testupdate",
                   "user_id" => 1,
                        "id" => 3,
                   "Details" => [
            [0] {
                        "task_id" => 3,
                   "break_length" => 4,
                      "completed" => false,
                             "id" => 12,
                         "length" => 25,
                    "location_id" => nil,
                           "note" => "test444",
                "start_date_time" => "2011-12-15T00:00:00Z"
            }
        ],
        "details_attributes" => [
            [0] {
                "start_date_time" => "2011-12-15T00:00:00Z",
                      "completed" => false,
                           "note" => "test444",
                   "break_length" => 4,
                        "task_id" => 3,
                             "id" => 12,
                         "length" => 25,
                    "location_id" => nil
            }
        ]
    }
}

僅供參考-我已覆蓋Task toJSON方法,以用Rails期望的“ _attributes”裝飾集合

另一方面,如果我在服務器上以老式的Rails方式(使用嵌套形式)執行此更改,則會發送嵌套對象的哈希(盡管在此示例中只有一個)(請查看Details_attributes):

{
                  "utf8" => "",
    "authenticity_token" => "iv9wYvgqLt3nldVOX4AeAifpFaSHIfEj85MsPUaMiAw=",
                  "task" => {
                      "name" => "test",
        "details_attributes" => {
            "0" => {
                       "_destroy" => "",
                "start_date_time" => "2011-12-15 00:00:00",
                         "length" => "25",
                      "completed" => "0",
                           "note" => "test444",
                   "break_length" => "4",
                             "id" => "12"
            }
        }
    },
                "commit" => "Update task",
               "user_id" => "1",
                    "id" => "3"
}

關於如何獲取我的json,進行更新以使服務器接受它的任何指導?

謝謝你的幫助。

您可以提供一個自定義同步方法來覆蓋默認的序列化。 例如(我希望離您的設置不太遠)

var json='{"name":"testupdate", "user_id":1, "id":3,  "details_attributes":[{"start_date_time":"2011-12-15T00:00:00Z", "completed":false, "note":"test444", "break_length":4, "task_id":3, "id":12, "length":25}]}';

Task = Backbone.Model.extend({
    initialize:function() {
        this.attrs=new DetailsAttributes(this.get("details_attributes"));
    },
    sync: function(method, model, options) {
        if (method == 'update') {
            var data = this.toJSON();
            data.details_attributes = {};
            this.attrs.each(function(model, ix) {
                data.details_attributes[ix] = model.toJSON();
            });

            console.log(JSON.stringify(data));

            options = _.extend({data: data}, options);
        }


        return Backbone.sync.call(this, method, this, options);
    }
});
DetailAttribute= Backbone.Model.extend();
DetailsAttributes= Backbone.Collection.extend({
    model:DetailAttribute
});
var tk= new Task(JSON.parse(json));
tk.save();

如果您要檢查控制台日志,請訪問http://jsfiddle.net/5gZr5/4/

Backbone.sync將使用選項中傳遞的data屬性進行序列化。

暫無
暫無

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

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