Backbone.js - 使用 Rails 和嵌套集合的指南
我在嵌套模型示例中使用 codebrew\backbone-rails (假设我有一个任务集合,每个任务可以有一个详细信息集合 - 与示例类似。)
我可以加载并创建一系列嵌套视图来显示我想要的数据,但现在在尝试对该数据执行 CRUD 操作时陷入困境。
例如 - 假设我更改了外部(上部?)对象的属性,并且想要将该数据发送到服务器。该 json 如下所示。由于我在加载应用程序时“急切地”加载了嵌套数据,因此我将在更新时将其发送回服务器(查看详细信息属性格式):
{
"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
}
]
}
}
仅供参考 - 我已经覆盖了任务 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的任何指导,使其看起来应该让服务器接受它?
感谢您的帮助。
I'm using codebrew\backbone-rails in a nested model example (say I have a collection of Tasks, and each can have a collection of Details - similar to the example.)
I can load and create a series of nested views to display the data I want, but now I'm stuck when trying to perform CRUD operations on that data.
For example - say I change an attribute on my outer(upper?) object, and want to send that data to the server. Here's what that json looks like. Since I "eagerly" loaded my nested data when I loaded the app, I'm going to send it back to the server on an update (look at details_attributes format):
{
"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
}
]
}
}
FYI - I've overridden the Task toJSON method to decorate the collection with "_attributes" that Rails expects
On the other hand, if I performed this change on the server, the old-fashioned rails way (using a nested form), I send a hash of nested objects (although there's only one in this example (look at 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"
}
any guidance on how to get my json, on an update, to look like it should for the server to accept it?
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以提供自定义同步方法来覆盖默认序列化。例如(我希望我离你的设置不太远)
http://jsfiddle.net/5gZr5/4 / 如果你想检查控制台日志。
Backbone.sync 将使用选项中传递的数据属性进行序列化。
You could provide a custom sync method to override the default serialization. For example (I hope I'm not too far from your setup)
http://jsfiddle.net/5gZr5/4/ if you want to check the console log.
Backbone.sync will use the data attribute passed in the options for its serialization.