简体   繁体   English

如何在Ember中实施HABTM?

[英]How to implement a HABTM in Ember?

Ive got the same HABTM (has many and belongs to many) as described at http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association Ive拥有与http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association中所述的相同的HABTM(有很多,属于很多)

In ember-data, how does one define the relationship between physician, appointment and patient? 在余烬数据中,如何定义医师,预约和患者之间的关系?

In Rails, it is easy enough via has many through. 在Rails中,通过许多遍是很容易的。 Or, is there no need to do a HABTM association in Ember, since it is pulling/sending data out from/to an API? 或者,因为它正在从API中提取/发送数据,所以不需要在Ember中进行HABTM关联吗?

Unfortunately, http://emberjs.com/guides/models/defining-models/#toc_many-to-many shows a many-to-many association with TWO models only. 不幸的是, http://emberjs.com/guides/models/defining-models/#toc_many-to-many仅显示了与两个模型的多对多关联。

Well, at a minimum you're going to need to add a couple of belongs-to relationships on your appointment model: 好吧,至少您需要在约会模型上添加几个“属于”关系:

App.Appointment = DS.Model.extend({
  ...
  physician: DS.belongsTo('physician'),
  patient: DS.belongsTo('patient'),
  ...
});

Thus, whenever an appointment is saved, its child relationships will be saved with it. 因此,无论何时保存约会,约会的子关系都将随之保存。 I assume that's what you want, since that's how the db is structured in the link you posted to the Rails guide. 我假设这就是您想要的,因为这就是您发布到Rails指南的链接中数据库的结构。

The rest depends heavily on how your application is structured, especially your server's JSON API. 其余部分在很大程度上取决于应用程序的结构,尤其是服务器的JSON API。 For example, if you've got a model physician and you might be able to do something like this: 例如,如果您有一名模型physician并且您可以执行以下操作:

var query = { physician: physician.get('id') };
this.get('store').findQuery('appointment', query).then(function (results) {
  ...
});

If you then wanted to find all of a physician's patients, you could simply return an array of the unique patients belonging to the appointments that were found. 然后,如果您想查找所有医师的患者,则可以简单地返回一系列属于所找到约会的独特患者。 This approach is pretty straightforward and easy to reason about, but it doesn't take full advantage of Ember Data. 这种方法非常简单易懂,但并没有充分利用Ember Data。

Alternatively, you could try defining a has-many relationship on your physician and patient models: appointments: DS.hasMany('appointment') , which has some advantages but also requires much better knowledge of Ember Data. 或者,您可以尝试在医师和患者模型上定义一个“多对多”的关系: appointments: DS.hasMany('appointment') ,它虽然有一些优点,但也需要对Ember Data有更好的了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM