繁体   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