Ember Rails模型关系 [英] Ember Rails model relationship

查看:164
本文介绍了Ember Rails模型关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一个构建在Rails API之上的Ember.js应用程序。这是最初运行以下:

  Ember.VERSION:1.0.0 ember.js 
DEBUG:Handlebars.VERSION :1.0.0 ember.js
DEBUG:jQuery.VERSION:1.10.2

这些更新到以下内容:

  Ember:1.7.1 + pre.f095a455 ember.js 
DEBUG: Ember数据:1.0.0-beta.10 + canary.30d6bf849b ember.js
DEBUG:Handlebars:1.3.0 ember.js

使用ember-rails gem。



当试图保存一个部分模型时,我陷入了一个大的阻止者问题。每个部分与行模型有关系:

  row:DS.belongsTo('row',{async:true}) 

和行模型作为回报:

 部分:DS.hasMany('section',{async:true})

保存新的部分在控制器中运行以下功能。在将该对象发送到createRecord方法之前,行类被发送并应用于对象。

  _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失败');
});

return new_section;
},

创建模型后,问题就会存在。该行实际上并不正确地应用于模型,因此Rails API请求不起作用,该部分未被保存。



保存在new_section对象中的数据在createRecord请求之前如下所示:

  html:< p>新部分< / p> 
位置:3
行:类
section_type_type:TextSection
width:6

这是上面代码中附加的行类(扩展_data节点):

  __ember1414408746094:ember791
__ember_meta__:Object
__nextSuper:undefined
_attributes:Object
_changesToSync:Object
_data:Object
id:1
页面: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

但是,以下是返回的new_section v中的_data节点ariable:

  _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>章节< / 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

可以看到行值为空。



我很难(大)试图弄清这个问题发生在哪里。这可能与Ember数据更新有关(最初使用的是较旧版本),但是我恐怕我不知道改变了什么,为什么会发生这种变化。



任何人都可以提供的帮助将非常有用,谢谢。

解决方案

找到的错误的地方,对象中有另一个键叫做 _relationships ,你应该找到那里,不在 _data



如果你设置创建记录后的行?

  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);


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.

解决方案

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);

这篇关于Ember Rails模型关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆