我試圖存儲與屬於關聯的關聯記錄,但相關的ID總是空:createRecord與屬於關聯關聯
客戶模式
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')
});
在ProjectsController中保存操作
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);
});
sended到服務器的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"
}}
我有同樣的問題,你能告訴我怎麼解決了這一問題? – JeskTop