【发布时间】:2015-02-14 16:41:02
【问题描述】:
我刚刚偶然发现了 Ember 数据多态关系。我认为它们对我的工作很有用。这是我的主要模型。该模型定义了与异步和多态的主题有很多关系。
App.Source = DS.Model.extend({
name: DS.attr('string'),
type: DS.attr('string'),
locationType: DS.attr('string'),
locationSpecific: DS.attr('boolean'),
primary: DS.attr('boolean'),
published: DS.attr('boolean'),
sort: DS.attr('number'),
topics: DS.hasMany('topic', {
async: true,
polymorphic: true
})
});
接下来我们有一个基类为“主题”的主题
App.Topic = DS.Model.extend({
name: DS.attr('string'),
sort: DS.attr('number'),
source: DS.belongsTo('source')
});
App.RegTopic = App.Topic.extend({
test: DS.attr('number', {
defaultValue: 8
}),
notes: DS.hasMany('notes', {
async: true
})
});
App.SummaryTopic = App.Topic.extend({
number: DS.attr('number', {
defaultValue: 9
})
});
这是我获取主题的方法
App.TopicsRoute = Ember.Route.extend({
model: function() {
return this.modelFor('source').get('topics');
}
});
当我列出来源时,我会得到以下对象的列表
{
id: 1
name: "test"
type: "testType"
locationType: "international"
locationSpecific: "true"
primary: true
published: true
sort: 1
links: {
topics: "/topics?sourceId=1"
}
}
然后我的主题调用会返回这些对象
{
id: 4
sourceId: 1
name: Topic 4
type: "regTopic"
sort: 1
}
我错过了什么吗?不能对'links'对象使用多态关系吗?
根据我对多态关系的理解,当我调用 /topics?sourceId=1 时,它本质上应该是在一次调用中加载 2 个不同的主题,这样我就可以在同一页面上显示 regTopic 和 summaryTopic 但分开显示列表并将它们作为单独的对象保存?
这是一个更新的 jsbin,似乎可以正常工作了。 http://emberjs.jsbin.com/medojitibo/1/edit?html,js,console,output
我看到的问题是,在我的控制器中,主题都是列表中的 App.Topic。没有 App.RegTopic 或 App.SummaryTopic
【问题讨论】:
-
1.究竟是什么问题? 2. 请通过emberjs.jsbin.com提供问题演示
标签: ember.js ember-data