Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone Model.save doesn't wrap model state to nested object

Tags:

backbone.js

Backbone Model.save doesn't wrap model state to nested object. Is it normal?

Typical scenario for Rails is something like this params[:product]. How could I achieve this?

like image 215
Alexey Zakharov Avatar asked May 20 '11 08:05

Alexey Zakharov


1 Answers

This was answered previously: Saving nested objects with Rails, backbone.js, and accepts_nested_attributes_for

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.

like image 82
Julien Avatar answered Oct 25 '22 09:10

Julien