gpt4 book ai didi

ember.js - 如何序列化ember数据中的嵌入式关联?

转载 作者:行者123 更新时间:2023-12-02 03:52:21 26 4
gpt4 key购买 nike

背景

最近Tom Dale announced “嵌入式加载现在回来了!”。然而,它似乎只在 loading 场景中得到完全支持。目前尚不清楚序列化嵌入式关联是否仍在开发中,或者是否希望用户通过构建自定义序列化程序自行实现。

要求

我的嵌入式关联的 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' )
});

尝试的解决方案

以下解决方案大部分都有效,但感觉我一定是“做错了”,因为我不得不对如此多的代码进行逆向工程和子类化。

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
})
})

请求反馈

  • 这是正确的道路吗?
  • ember 团队是否正在积极研究更好的方法来做到这一点?

最佳答案

查看 ember-data 的 embedded-records 分支: https://github.com/emberjs/data/commits/embedded-records

特别是,请参阅此提交以保存嵌入数据: https://github.com/emberjs/data/commit/0abd3b965c50dfeb23bd8ff50751825482050e68

对嵌入式记录的全面支持指日可待:)

更新

Embedded records 分支于 2012 年 12 月 28 日合并到 master 中:

https://github.com/emberjs/data/commit/b5d7c478e79aa9706e0196b8769b7ef67bb26fc4

关于ember.js - 如何序列化ember数据中的嵌入式关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13897307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com