关于使用rails和嵌套集合的指导

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

本文关键字:集合 嵌套 于使用 rails      更新时间:2023-09-26

我在一个嵌套模型示例中使用codebrew'backbone-rails(假设我有一个任务集合,每个任务可以有一个细节集合-类似于示例)

我可以加载并创建一系列嵌套视图来显示我想要的数据,但是现在当我试图对该数据执行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/如果你想检查控制台日志。

骨干。Sync将使用在序列化选项中传递的data属性。