1
我有一套名爲Profile
和Tag
的模型。配置文件可以有很多標籤,並且標籤可以屬於多個配置文件。我的模型設置如下:Ember.js保存多對多關係到服務器
App.Profile = DS.Model.extend({
name: DS.attr(),
tags: DS.hasMany('tag')
});
App.Tag = DS.Model.extend({
title: DS.attr(),
profile: DS.hasMany('profile')
});
我寫了下面的代碼來測試關係和數據提交到服務器:
var profile = this.store.createRecord('profile', {
name: 'John Doe'
});
var tag1 = this.store.createRecord('tag', {
title: 'Tag 1'
});
var tag2 = this.store.createRecord('tag', {
title: 'Tag 2'
});
var tag3 = this.store.createRecord('tag', {
title: 'Tag 3'
});
profile.get('tags').pushObject(tag1);
profile.get('tags').pushObject(tag2);
profile.get('tags').pushObject(tag3);
profile.save();
然而,關係永遠不會發送到服務器,即使我先保存標籤,然後再保存配置文件。
無論什麼數據灰燼POST到/profiles/
總是包含"tags": [ null, null, null ]
編輯:我是救了我的模型以錯誤的方式,此代碼的工作對我來說:
profile.get('tags').save().then(function() {
profile.save();
});
謝謝,原來我是以錯誤的方式保存標籤。 –