Using this.store.filter with the ID of the belongs_to model

234 views Asked by At

I'm trying to retrieve all the layouts for a given account.

/app/models/account.js

import DS from 'ember-data';

export default DS.Model.extend({
  companyName: DS.attr('string'),
  layouts: DS.hasMany('layout')
});

/app/models/layout.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  account: DS.belongsTo('account', { async: true })
});

/app/routes/layouts.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    return Ember.RSVP.hash({
      layouts: this.store.filter('layout', { account_id: 1 }, function(layout) {
        console.log(layout.get('account').content.id);
        return layout.get('account').content.id === 1;
      })
    });
  }
});

The console.log line is outputting the ID that I'm expecting (1). In Ember inspector I can see 5 layout models and under 'Belongs To' I can see: account : <DS.PromiseObject:ember960>. Clicking that brings up content : <batmics@model:account::ember600:1> and clicking that brings up the properties, including the correct ID.

But in my templates layouts is empty... and I've no idea why.

Incidentally, layouts: this.store.find('layout', { account_id: 1 }) works, but I need it to use the filter so that it's an active array.

1

There are 1 answers

0
jmurphyau On BEST ANSWER

Ember Data works with all its IDs as strings.

Changing your check to === '1' should get this going for you.

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    return Ember.RSVP.hash({
      layouts: this.store.filter('layout', { account_id: 1 }, function(layout) {
        console.log(layout.get('account').content.id);
        return layout.get('account').content.id === '1';
      })
    });
  }
});