VoloD
VoloD

Reputation: 77

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".

But the main question is: how do I check whether the course is already inside "model.courses"?

Upvotes: 0

Views: 1086

Answers (1)

RhinoWalrus
RhinoWalrus

Reputation: 3089

DS.hasMany instantiates a DS.ManyArray, which extends a DS.RecordArray, which extends a run-of-the-mill Em.ArrayProxy. You should be able to do a courses.contains(test object) to see if it's already in the collection. Adding and removing courses should just be a matter of using pushObject and removeObject:

courses.pushObject(object);

...

courses.removeObject(object);

Upvotes: 3

Related Questions