mercredi 2 septembre 2015

How to filter records using hasMany association

Trying to figure out what should be a simple solution: filterBy records using an array of hasMany ids.

I have a model Language, which hasMany Providers, and vice versa. From a collection of Providers, I need to filterBy('language', [1,2]) or even filterBy('language', 2).

(I can't do a language.get('providers') since I'd getting the collection of Providers from another association.)

// models.language.js
var Language = DS.Model.extend({
  title: DS.attr('string'),
  providers: DS.hasMany('provider')
});

// models/provider.js
var Provider =  DS.Model.extend({
    title: DS.attr('string'),
    region: DS.belongsTo('region', { async: true }),
    languages: DS.hasMany('language', {async: true})
}); 

// In this region-component.js I have the model title, 
// and then an each block listing all providers
// When the language filter changes, I need it to filter down
// just to those providers who have the language association.
providers: function() { 
    return this.get('model').get('providers').filterBy('languages', this.get('language'));
}.property('model')




Aucun commentaire:

Enregistrer un commentaire