【问题标题】:Ember Rails model relationshipEmber Rails 模型关系
【发布时间】:2014-10-27 10:52:26
【问题描述】:

我继承了一个基于 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 Data : 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 })

以及作为回报的行模型:

sections: 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 failure');
  });

  return new_section;
},

模型一创建,问题就存在。该行实际上并未正确应用于模型,因此 Rails API 请求无法正常工作,并且该部分未保存。

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

html: "<p>New section</p>"
position: 3
row: Class
section_type_type: "TextSection"
width: 6

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

__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

但是,以下是返回的 new_section 变量中的 _data 节点:

_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

这里可以看到行值为null。

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

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

【问题讨论】:

    标签: ruby-on-rails ember.js ember-data


    【解决方案1】:

    一方面,您正在寻找错误的位置来找到row,在名为_relationships 的对象中还有另一个键,您应该在那里找到row,而不是在_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);
    

    【讨论】:

      猜你喜欢
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      相关资源
      最近更新 更多