使用Rails、backbone.js 和accepts_nested_attributes_for 保存嵌套对象
我正在使用 Rails、backbone.js(现在正在学习)。假设您有两个模型:汽车和发动机。
var Car = Backbone.Model.extend({
initialize: function() {
if(this.get('engine') != undefined) this.engine = new Engine(this.get('engine'));
}
}
var redCar = new Car({
'color': 'red',
// The controller nests the model
'engine': {
'horsepower': '350'
}
});
redCar.save()
将 engine_attributes
发送到控制器的正确方法是什么? (汽车 accepts_nested_attributes_for :engine
,因此它需要 engine_attributes
。)我是否覆盖 Backbone sync()
?对于嵌套模型是否有更好的约定可以遵循?
也许我不应该从控制器返回嵌套模型,或者返回 engine_attributes
而不是 engine
?
顺便说一句,我正在使用 Rails respond_with(@car, :include => :engine)
(与 @car.to_json(:include => :engine)<事实上,这个 api 将引擎属性嵌套在
engine
下,但模型需要 engine_attributes
似乎是矛盾的 - 我从来不知道如何协调这一点。
I'm using Rails, backbone.js (learning this now). Let's say you have two models, Car and Engine.
var Car = Backbone.Model.extend({
initialize: function() {
if(this.get('engine') != undefined) this.engine = new Engine(this.get('engine'));
}
}
var redCar = new Car({
'color': 'red',
// The controller nests the model
'engine': {
'horsepower': '350'
}
});
redCar.save()
What is the right way to send engine_attributes
to the controller? (Car accepts_nested_attributes_for :engine
, so it expects engine_attributes
.) Do I override the Backbone sync()
? Is there a better convention to follow for nested models?
Maybe I should not be returning nested models from the controller, or returning engine_attributes
instead of engine
?
On a side note, I am using the Rails respond_with(@car, :include => :engine)
(same as @car.to_json(:include => :engine)
. The fact that this api nests the engine attributes under engine
but the model expects engine_attributes
seems contradictory - I've never been sure how to reconcile this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议在主干模型上覆盖 toJSON。
toJSON 在将数据发送到后端之前在同步方法中调用。
I would suggest to override toJSON on the backbone model.
toJSON is called within the sync method just before sending data to the backend.
非常有帮助。我正在处理类似的情况,这个例子一开始对我不起作用。在我的示例中,我有一个 has_many/belongs_to 关系。例如,汽车 has_many :轮胎。
我遇到的问题是,ties_attributes 需要嵌套在汽车 json 的内部,而不是与其相邻。我最终得到了这样的结果:
希望这会有所帮助。
Very helpful. I was working with a similar situation, and this example did not work for me at first. In my example I have a has_many / belongs_to relation. For example a car has_many :tires.
The problem I encountered was that the tires_attributes needed to be nested INSIDE of the car json, not adjacent to it. I ended up with something like this:
Hope this helps.
我找到了这颗宝石!也许对你有帮助。
http:// blog.dtmtec.com.br/blog/2013/05/power-up-your-backbone-models-with-nested-attributes/
I found this gem! Maybe help you.
http://blog.dtmtec.com.br/blog/2013/05/power-up-your-backbone-models-with-nested-attributes/
我相信我们必须对(或围绕)Backbone
sync
或 Rails 后端做一些事情,因为两者之间存在通信问题。重写 toJSON() 方法可能会导致意外结果,因为这是通用方法,应用程序的其他部分可能依赖,例如视图。可能的快速解决方案:
I believe we've to do something with (or around) Backbone
sync
or with rails backend, because the problem in communication between two. OverridingtoJSON()
method might lead to unexpected results, because this is general purpose method and others parts of application might rely on, for example views.Possibly quick solution: