我有一个基于CouchDB
的后端。与每个基于文档的商店一样,深层嵌套的属性是一等公民。这意味着,嵌套属性不会必然引用独立对象。通常,嵌套属性仅用于构造复杂对象,而不暗示任何belongsTo
或hasMany
关系。这样做的一个直接后果是嵌套的“对象”(它们不是真正的对象)没有ID。
由于ember-data不直接支持嵌套属性,因此直到现在我一直使用belongsTo
关系建模这些对象,并且成功。这是我从后端收到的数据结构:
{
"agents": [
{
"_id": "04ef35dd-6ab9-492f-ada6-0d4f4de9610a",
"genproperties": {
"playable-url": null
},
"org_id": 11653,
"outputs": {
"exten": null,
"extension": "c4facb9d-850c-4b67-9592-b467129fa9d4",
"jumpIfBusy": null,
"jumpIfNoAnswer": null,
"nextnode": "a1e00fb9-8604-4119-8515-3a31a36c60d1",
"startnode": null
},
"properties": {
"agent_status": "online",
"allowed_portlets": [
"xxx-portlet",
"yyy-portlet",
"zzz-portlet",
"uuu-portlet",
"ppp-portlet"
],
"default_portlet": "xxx-portlet",
"email": "a.b@c.com",
"enabled_portlets": [
"xxx-portlet",
"yyy-portlet",
"zzz-portlet",
"uuu-portlet",
],
"first_name": "John",
"main-service": null,
"name": "John Ferdinand Smith",
"portlet_language": "en",
"pov-org": null,
"services": {
"associated": {}
},
"surname1": "Ferdinand",
"surname2": "Smith"
},
"type": "wav-user"
},
...
]
}
这是我的适配器:
App.Adapter = DS.RESTAdapter.extend({
bulkCommit: false,
namespace: App.config.API_NAMESPACE,
url: mynamespace.apiUrl,
serializer: App.MetaRESTSerializer.create(),
ajax: function(url, type, hash) {
var ajaxPromise = this._super(url, type, hash);
if (App.config.DEBUG_ADAPTER) { console.log('>>>>> REQUESTED > %s:%s > hash=%o ajaxPromise=%o', type, url, hash, ajaxPromise); }
ajaxPromise.then(function(json){
if (App.config.DEBUG_ADAPTER) { console.log('>>>>> RECEIVED > %s:%s > %o', type, url, json); }
});
return ajaxPromise;
}
});
这些是我目前的模型定义:
App.NodeOutputs = DS.Model.extend({
startnode : DS.attr('string'),
jumpIfBusy : DS.attr('string'),
jumpIfNoAnswer : DS.attr('string'),
nextnode : DS.attr('string'),
extension : DS.attr('string'),
exten : DS.attr('string'),
});
App.NodeMixin = Ember.Mixin.create({
nodeType : DS.attr('string'),
nodeSubtype : DS.attr('string'),
outputs : DS.belongsTo('App.NodeOutputs'),
});
App.Adapter.map('App.GenProperties', {
playableUrl : {key: 'playable-url'},
});
App.AgentProperties = App.CommonProperties.extend({
default_portlet : DS.attr('string'),
allowed_portlets : DS.attr('rawTransform'),
portlet_language : DS.attr('string'),
verified_emails : DS.attr('rawTransform'),
email : DS.attr('string'),
agent_status : DS.attr('string'),
});
App.Agent = DS.Model.extend(App.NodeMixin, {
properties : DS.belongsTo('App.AgentProperties'),
genproperties : DS.belongsTo('App.GenProperties'),
});
这些是我的映射:
App.Adapter.map('App.NodeOutputs', {
startnode : {key: 'startnode'},
jumpIfBusy : {key: 'jumpIfBusy'},
jumpIfNoAnswer : {key: 'jumpIfNoAnswer'},
nextnode : {key: 'nextnode'},
extension : {key: 'extension'},
exten : {key: 'exten'},
});
App.Adapter.map('App.Agent', {
nodeType : { key: 'type' },
nodeSubtype : { key: 'subtype' },
outputs : { embedded: 'always' },
properties : { embedded: 'always' }
genproperties : { embedded: 'always' }
});
这与ember-data#0.0.14
完美配合。现在我转到ember-data#1.0.0-beta.3
,似乎不再支持DS.RESTAdapter.map
。
我看过这些资源:
但我仍然不知道如何完成这两个简单的任务:
type
- > nodeType
(json - > ember模型)belongsTo
为embedded: 'always'
问题:是否有一些示例代码展示了最新的ember-data的键映射和嵌入属性?它是否支持嵌入式属性没有 ID?
我有30多个映射要迁移,所以在开始之前我最好先了解一下我在做什么。