简体   繁体   English

Ember-data:如何检查hasMany关系中是否存在记录?

[英]Ember-data: how to check whether record is present in hasMany relation?

I have two models: 我有两个模型:

App.Administrator = DS.Model.extend({
  name:    DS.attr('string'),
  courses: DS.hasMany('course', {async: true})
});

App.Course = DS.Model.extend({
  title: DS.attr('string')
})

On "edit administrator" page I want to display list of checkboxes, one for each course, so that selecting one pushes it to "model.courses", and unselecting removes it from "model.courses". 在“编辑管理员”页面上,我要显示复选框列表,每个课程一个,以便选择一个将其推到“ model.courses”,而取消选择则将其从“ model.courses”中删除。

But the main question is: how do I check whether the course is already inside "model.courses"? 但主要问题是:如何检查课程是否已经在“ model.courses”内部?

DS.hasMany instantiates a DS.ManyArray, which extends a DS.RecordArray, which extends a run-of-the-mill Em.ArrayProxy. DS.hasMany实例化一个DS.ManyArray,该实例扩展了DS.RecordArray,该实例扩展了现成的Em.ArrayProxy。 You should be able to do a courses.contains(test object) to see if it's already in the collection. 您应该能够进行一个courses.contains(test object)以查看它是否已经在集合中。 Adding and removing courses should just be a matter of using pushObject and removeObject: 添加和删​​除课程应该只是使用pushObject和removeObject的问题:

courses.pushObject(object);

... ...

courses.removeObject(object);

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

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