使用Rails、backbone.js 和accepts_nested_attributes_for 保存嵌套对象

发布于 2024-10-20 21:18:50 字数 932 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤云独去闲 2024-10-27 21:18:50

我建议在主干模型上覆盖 toJSON。

toJSON: function(){

  json = {car : this.attributes};
  return _.extend(json, {engine_attributes: this.get("engine").toJSON());

}

toJSON 在将数据发送到后端之前在同步方法中调用。

I would suggest to override toJSON on the backbone model.

toJSON: function(){

  json = {car : this.attributes};
  return _.extend(json, {engine_attributes: this.get("engine").toJSON());

}

toJSON is called within the sync method just before sending data to the backend.

东风软 2024-10-27 21:18:50

非常有帮助。我正在处理类似的情况,这个例子一开始对我不起作用。在我的示例中,我有一个 has_many/belongs_to 关系。例如,汽车 has_many :轮胎。

我遇到的问题是,ties_attributes 需要嵌套在汽车 json 的内部,而不是与其相邻。我最终得到了这样的结果:

toJSON: function(){

  json = {car : this.attributes};
  json.car.tires_attributes = this.get('tires').toJSON();
  return 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:

toJSON: function(){

  json = {car : this.attributes};
  json.car.tires_attributes = this.get('tires').toJSON();
  return json;

}

Hope this helps.

小耗子 2024-10-27 21:18:50

我相信我们必须对(或围绕)Backbone sync 或 Rails 后端做一些事情,因为两者之间存在通信问题。重写 toJSON() 方法可能会导致意外结果,因为这是通用方法,应用程序的其他部分可能依赖,例如视图。

可能的快速解决方案:

redCar.save({}, {
    contentType: 'application/json',
    data: JSON.stringify({car: redCar.toJSON(), engines_attributes: redCar.get('engines').toJSON()})
});

I believe we've to do something with (or around) Backbone sync or with rails backend, because the problem in communication between two. Overriding toJSON() 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:

redCar.save({}, {
    contentType: 'application/json',
    data: JSON.stringify({car: redCar.toJSON(), engines_attributes: redCar.get('engines').toJSON()})
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文