gpt4 book ai didi

ember.js - ember-data 中的多态关系

转载 作者:行者123 更新时间:2023-12-02 06:00:23 25 4
gpt4 key购买 nike

我有三个型号:公司 , , 和 任务 .一个公司有很多人。一个人拥有一家公司。一个公司有很多任务。一个人有很多任务。

任务关系是多态的。

这是我的模型

App.Taskable = DS.Model.extend({

tasks: DS.hasMany('task')
});

App.Task = DS.Model.extend({

subject: DS.attr('string'),

taskable: DS.belongsTo('taskable', { polymorphic: true})
});

App.Person = App.Taskable.extend({

firstName: DS.attr('string'),

lastName: DS.attr('string'),

email: DS.attr('string'),

company: DS.belongsTo('company'),

fullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
}.property('firstName', 'lastName')

});

App.Company = App.Taskable.extend({

name: DS.attr('string'),

people: DS.hasMany('person')
});

请注意 公司 延长 可任务 .我相信我已经正确定义了这些关系。我不知道如何延迟加载任务。

这是我的个人观点
  <script type="text/x-handlebars" data-template-name='show/_person'>
<div>
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<p class="form-control-static">{{fullName}}</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Company</label>
<div class="col-sm-10">
<p class="form-control-static">{{company.name}}</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Tasks</label>
<div class="col-sm-10">
<p class="form-control-static">
{{#each task in tasks}}
{{task.subject}}<br />
{{/each}}
</p>
</div>
</div>
</script>

为与此人关联的公司发出 GET 请求,但不为任务发出请求。我如何获得与个人或公司相关的任务?我希望向 people/3/tasks 发出 GET 请求或类似的东西

最佳答案

我认为只是 ActiveModelAdapter已经实现了多态关联。

要使其正常工作,您需要使用以下格式:
GET /tasks

{
tasks: [
{
id: 1,
subject: 'Mytask1',
// in the polymorphic association we need to say the type and the id
taskable: { type: "person", id: 1 }
},
{
id: 2,
subject: 'Mytask2',
taskable: { type: "company", id: 1 }
}
]
}
GET /tasks/1
{
task: {
id: 1,
subject: 'Mytask1',
// in the polymorphic association we need to say the type and the id
taskable: { type: "person", id: 1 }
}
}

我在 fiddle 中更新了您的示例,请看一下 http://jsfiddle.net/marciojunior/7k7RT/

关于ember.js - ember-data 中的多态关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20208075/

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