最近Tom Dale announced"嵌入式加载现已回来了!"。但是,它似乎完全支持加载方案。目前还不清楚序列化嵌入式关联是否仍处于开发阶段,或者是否希望用户通过构建自定义序列化程序来实现它。
我的嵌入式关联的JSON如下所示:
{
inputs: [
{
id: 1,
name: "Favorite Color",
type: "SelectInput",
options: {
choices: [ 'red', 'green', 'yellow' ]
}
},
{
id: 2,
name: "email",
type: "TextInput",
options: {}
}
]
}
我相信" ember方式"表示这是为了构建一个自定义序列化程序,将我的JSON转换为如下模型:
App.Choice = DS.Model.extend({
name: DS.attr( 'string' ),
input: DS.belongsTo( 'App.Input' )
});
App.Input = DS.Model.extend({
name: DS.attr( 'string' ),
type: DS.attr( 'string' ),
choices: DS.hasMany( 'App.Choice' )
});
以下解决方案大多有效,但感觉我必须做错了#34;因为我不得不对这么多代码进行逆向工程和子类化。
Customizer.MyRESTAdapter = DS.RESTAdapter.extend({
dirtyRecordsForAttributeChange: function(dirtySet, record, attributeName, newValue, oldValue) {
if(record.constructor === Customizer.Choice) {
if(newValue === oldValue) { return; }
var input = null;
if (attributeName == 'name') {
input = record.get('input');
}
else if(attributeName == 'input') {
input = newValue;
}
if( input ) {
dirtySet.add( input );
}
}
else {
this._super(dirtySet, record, attributeName, newValue, oldValue);
}
},
dirtyRecordsForBelongsToChange: function(dirtySet, child, relationship) {
if(child.constructor === Customizer.Choice) {
var input = child.get( 'input' );
if( input ) {
dirtySet.add( input );
}
}
else {
this._super(dirtySet, child, relationship);
}
},
dirtyRecordsForHasManyChange: function(dirtySet, parent, relationship) {
this._super(dirtySet, parent, relationship);
}
});
Customizer.MyRESTSerializer = DS.RESTSerializer.extend({
init: function() {
this._super();
this.mappings.set( 'Customizer.Input', { choices: { embedded: 'load' } } );
},
extractEmbeddedHasMany: function(type, hash, key) {
if(type == Customizer.Input) {
if(!(hash['options'] && hash['options']['choices'])) { return null; }
var choices = [];
hash['options']['choices'].forEach(function(choice, i){
var choiceId = hash['id'] + '_' + i;
var inputId = hash['id'];
choices[i] = { id: choiceId, input_id: inputId, name: choice };
});
return choices;
}
return this._super(type, hash, key);
},
addHasMany: function(data, record, key, relationship) {
this._super(data, record, key, relationship);
if( key === 'choices' ) {
var choices = record.get('choices').map(function( choice ){
return choice.get( 'name' );
});
data['options'] = data['options'] || {};
data['options']['choices'] = choices;
}
}
});
Customizer.store = DS.Store.create({
revision: 10,
adapter: Customizer.MyRESTAdapter.create({
namespace: 'api/v1',
bulkCommit: false,
serializer: Customizer.MyRESTSerializer
})
})
答案 0 :(得分:1)
查看ember-data的embedded-records分支: https://github.com/emberjs/data/commits/embedded-records
特别是,请参阅此提交以保存嵌入数据: https://github.com/emberjs/data/commit/0abd3b965c50dfeb23bd8ff50751825482050e68
对嵌入式记录的全面支持即将来临:)
<强>更新强>
嵌入式记录分支于12/28/2012合并为主数据集:
https://github.com/emberjs/data/commit/b5d7c478e79aa9706e0196b8769b7ef67bb26fc4
答案 1 :(得分:0)
我最终使用http://mozmonkey.com/2013/12/serializing-embedded-relationships-ember-data-beta/的变体。在我的情况下,我将id
添加到JSON,或者如果它不脏,我只是自己发送id
。
class App.ApplicationSerializer extends DS.ActiveModelSerializer
# Based on http://mozmonkey.com/2013/12/serializing-embedded-relationships-ember-data-beta/
# Also see http://discuss.emberjs.com/t/ember-data-beta-3-and-saving-hasmany-relationships-with-activemodelserializer`-and-rails/3167
serializeHasMany: (record, json, relationship) ->
key = relationship.key
hasManyRecords = Ember.get(record, key)
embed = relationship.options.embed
if embed && hasManyRecords
json[key] = []
hasManyRecords.forEach (item, index) =>
json[key].push @serializeItem(item, embed)
else
@._super(record, json, relationship)
serializeBelongsTo: (record, json, relationship) ->
key = relationship.key
belongsToRecord = Ember.get(record, key)
embed = relationship.options.embed
if embed
json[key] = @serializeItem(belongsToRecord, embed)
else
@._super(record, json, relationship)
serializeItem: (item, embed) ->
if embed == 'ifDirty'
if item.isDirty
data = item.serialize()
else
data = {}
data['id'] = item.id
else if embed == 'always'
data = item.serialize()
data['id'] = item.id
else
null
data