问题
I have inherited an Ember.js application built on top of a Rails API. This was originally running the following:
Ember.VERSION : 1.0.0 ember.js
DEBUG: Handlebars.VERSION : 1.0.0 ember.js
DEBUG: jQuery.VERSION : 1.10.2
but these have been updated to the following:
Ember : 1.7.1+pre.f095a455 ember.js
DEBUG: Ember Data : 1.0.0-beta.10+canary.30d6bf849b ember.js
DEBUG: Handlebars : 1.3.0 ember.js
using the ember-rails gem.
I am falling into a big blocker issue when trying to save a section model. Each section has a relationship with the row model:
row: DS.belongsTo('row', { async: true })
and the row model in return:
sections: DS.hasMany('section', { async: true })
Saving a new section runs the following function in a controller. The row class is being sent through and applied to an object before that object is being sent into the createRecord method.
_createSection: function(row, new_section_data){
var store = this.get('store');
new_section_data.width = this.getNewSectionWidth(row);
new_section_data.position = this.getNewSectionPosition(row);
new_section_data.row = row;
var new_section = store.createRecord('section', new_section_data);
console.log(' returned new_section ');
console.log(new_section);
new_section.save().then(function () {
console.log('saved');
new_section.set('set_as_selected', true);
console.log(' new_section post save ');
console.log(new_section);
}, function () {
console.log('new section save failure');
});
return new_section;
},
The problem exists as soon as the model is created. The row is not actually being applied correctly to the model and therefore the Rails API request is not working and the section is not being saved.
The data held in the new_section object prior to the createRecord request is as below:
html: "<p>New section</p>"
position: 3
row: Class
section_type_type: "TextSection"
width: 6
This is the row class attached in the above code (with expanded _data node):
__ember1414408746094: "ember791"
__ember_meta__: Object
__nextSuper: undefined
_attributes: Object
_changesToSync: Object
_data: Object
id: 1
page: Class
page_id: 1
position: 1
sections: Array[2]
style: null
__proto__: Object
_deferredTriggers: Array[0]
_hasHadSections: true
_inFlightAttributes: Object
_relationships: Object
_updatingRecordArraysLater: false
container: Container
currentState: Object
defaultStyleObject: Object
id: "1"
store: Class
updateTimeout: 7
__proto__: Class
section_type_type: "TextSection"
width: 6
However, the following is the _data node from the returned new_section variable:
_data: Object
contact_form_fields: Array[0]
created_at: "2014-10-27T11:14:39.000Z"
deleted_at: null
gallery_images: Array[0]
heading_visible: false
height: null
html: "<p>New section</p>"
id: 21
position: 3
row: null
section_type_id: 19
section_type_type: "TextSection"
style: null
updated_at: "2014-10-27T11:14:39.000Z"
width: 6
Here you can see the row value is null.
I am having difficulty (big) trying to figure out where this issue is happening. This may be related to the Ember data updates (this was originally built using a much older version) but I'm afraid I dont have the knowledge to understand what changed and why this is happening.
Any help anyone can provide would be very useful, thank you.
回答1:
One one hand you are looking at the wrong place to find the row
, there is another key in the object called _relationships
you should find row
there, not in _data
.
Does it work if you set the row after creating the record?
var store = this.get('store');
new_section_data.width = this.getNewSectionWidth(row);
new_section_data.position = this.getNewSectionPosition(row);
var new_section = store.createRecord('section', new_section_data);
new_section.set('row', row);
来源:https://stackoverflow.com/questions/26585922/ember-rails-model-relationship