Ember.js createRecord与belongsTo关联
我正在尝试存储一个与belongsTo关联的记录,但关联的ID始终为空: 客户模型Ember.js createRecord与belongsTo关联,ember.js,ember-data,Ember.js,Ember Data,我正在尝试存储一个与belongsTo关联的记录,但关联的ID始终为空: 客户模型 Docket.Customer = DS.Model.extend({ name: DS.attr('string'), initial: DS.attr('string'), description: DS.attr('string'), number: DS.attr('string'), archived: DS.attr('boolean'),
Docket.Customer = DS.Model.extend({
name: DS.attr('string'),
initial: DS.attr('string'),
description: DS.attr('string'),
number: DS.attr('string'),
archived: DS.attr('boolean'),
projects: DS.hasMany('project',{ async: true })
});
Docket.Project = DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
number: DS.attr('string'),
archived: DS.attr('boolean'),
customer: DS.belongsTo('customer')
});
项目模型
Docket.Customer = DS.Model.extend({
name: DS.attr('string'),
initial: DS.attr('string'),
description: DS.attr('string'),
number: DS.attr('string'),
archived: DS.attr('boolean'),
projects: DS.hasMany('project',{ async: true })
});
Docket.Project = DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
number: DS.attr('string'),
archived: DS.attr('boolean'),
customer: DS.belongsTo('customer')
});
在项目控制器中保存操作
var _this = this;
// create new record
var project = this.store.createRecord('project', this.getProperties('name','number', 'description', 'archived'));
// add customer with id 22 to project
this.store.find('customer', 22).then(function(customer) {
project.set('customer', customer);
});
// save if validation passes, otherwise show errors
project.save().then(function(){
_this.resetAndCloseForm(form);
}, function(response){
_this.set('errors',response.errors);
});
发送到服务器的json始终如下所示:
archived: false
customer_id: null
description: null
name: "foobar"
number: null
我想在选择框中选择项目的客户。是否有任何聪明的方法将所选客户作为记录获取,或者我必须将其加载到ID上
{{view Ember.Select
viewName="select"
contentBinding="customers"
optionLabelPath="content.name"
optionValuePath="content.id"
prompt="Pick a customer:"
selectionBinding="selectedCustomer"
}}
这是旧版本的余烬数据,还是您有自定义序列化程序,因为它应该在没有
\u id
的情况下发送它。尝试升级或显示序列化程序
您正在解决查找之前进行保存
// add customer with id 22 to project
this.store.find('customer', 22).then(function(customer) {
project.set('customer', customer);
// save if validation passes, otherwise show errors
project.save().then(function(){
_this.resetAndCloseForm(form);
}, function(response){
_this.set('errors',response.errors);
});
});
看起来selectedCustomer
有记录
// create new record
var project = this.store.createRecord('project', this.getProperties('name','number', 'description', 'archived'));
project.set('customer', this.get('selectedCustomer'));
// save if validation passes, otherwise show errors
project.save().then(function(){
_this.resetAndCloseForm(form);
}, function(response){
_this.set('errors',response.errors);
});
您还可以设置
选项valuepath='content'
是的,我现在在解决find后保存记录,但我想知道是否有更简单的方法来实现这一点。有没有办法直接从选择框中获取客户记录?我使用的是Ember Data 1.0.0 Beta 4。我正在使用这个App.ApplicationSerializer=DS.ActiveModelSerializer.extend({})代码>此外,我希望Ember向我的API发送customer\u id
。就像一个符咒:)我如何在编辑模式/覆盖表单上预先填充此选择框?它适用于所有其他字段,但没有填充选择框:var project=this.store.find('project',id.),然后(function(data){u this.set('selectedCustomer',data.get('customer');})代码>或者请看这里:我有同样的问题,你能告诉我如何解决这个问题吗?