vendredi 11 décembre 2015

How to use Ember model attributes in controller?

I have a model:

export default DS.Model.extend({
  name: DS.attr('string'),
  things: DS.hasMany('match', {async: true, defaultValue: []})
});

I can iterate through the things in a template:

{{#each model.things as |thing|}}
    <h3>thing.title</h3>
{{/each}}

But I don't want all of the things. I only want to display some of them. And which I will display will depend on a controller variable. So my thought is that I would have a computed property in the controller, as follows:

export default Ember.Controller.extend({
  state: 0,
  displayThings: function() {
    var things = this.get('model').things;
    var state = this.get('state');
    // do stuff, filter in a way that isn't important
    return things;
  }.property('state')
});

Forgetting the filtering for now, if I try to iterate through these displayThings as follows:

{{#each displayThings as |thing|}}
    <h3>thing.title</h3>
{{/each}}

Nothing shows up in the template. I can verify with console.log that the things are in the controller property, and some object is getting sent to the template, but not an object that can be iterated, somehow.

I'm using Ember 1.13.




Aucun commentaire:

Enregistrer un commentaire