如何在ember-data 1.0中提交包含相关模型数据的表单?

时间:2013-12-03 01:48:26

标签: ember.js ember-data

我正在从表单数据和另一个承诺(相关记录)创建记录。

这是我以

为中心的基本JSON
//appointment
{
  "id": 6,
  "details": "test",
  "customer": 1
}

//customer
{
  "id": 1,
  "name": "Sle eep",
  "appointments": [6]
}

我的余烬数据模型看起来像这样

App.Appointment = DS.Model.extend({
  details: attr('string'),
  customer: belongsTo('customer', { async: true})
});

App.Customer = DS.Model.extend({
  name: attr('string'),
  appointments: hasMany()
});

当我创建约会时,它目前看起来像这样

this.store.find('customer', 1).then(function(customer) {
  var appointment = {
    details: 'foo',
    customer: customer.get('id')
  }
  this.store.createRecord('appointment', appointment).save();
});

上面的问题是当表单数据是一个承诺时,我的序列化程序不能很好地完成。这是我应该如何创建记录?如果没有,这个创建应该是什么样的?

提前谢谢

更新

再看一遍之后,似乎isync:true属于belongsTo可能是问题。就在“createRecord”之前,我可以看到以下内容

  

对象{详细信息:“asd”,客户:1}

但是当我跳进ember-data的“createRecord”方法(在RESTAdapter中)时,我注意到现在客户再次被表示为一个promise(而不是我在调用该方法之前看到的整数值或字符串值)

1 个答案:

答案 0 :(得分:2)

为什么不在创建记录和发送之前等待查找解决?

var self = this,
    promise = this.store.find('customer', 1); //just to show it's a promise

promise.then(function(customer){

  var appointment = {
    details: 'foo',
    customer: customer
  }
  self.store.createRecord('appointment', appointment).save();
},{
  alert("uh oh end of the world, couldn't find customer");
});

Async的记录并不是很好,并且似乎有一些无关紧要的副作用(它仍处于测试阶段,所以我还没有做出重大判断)。话虽这么说,你的约会还没有定义,这里有一个游乐场。

http://emberjs.jsbin.com/OlaRagux/5/edit