lundi 14 décembre 2015

How to lookup associative array/object value in Ember template using string key

I know that the traditional syntax for looking up array values (by key) in Ember is to use .[] syntax, like:

{{myArray.[0]}}

However, I have an associative array, and despite having a separate model/controller property that lists the keys, I'm unable to perform a lookup - the value is always undefined.

As an example, here is a simple controller:

App.IndexRoute = Ember.Route.extend({
    model: function () {
        var ret = [];
        ret['notGreat'] = ['Plain Yogurt', 'Clams'];
        ret['great'] = ['Pizza', 'Chicken'];
        return Ember.RSVP.hash({
            keys: ['great', 'notGreat'],
            array: ret
        });

    }
});

And my template uses:

    {{#each key in model.keys}}
      <li>{{key}}</li>
      {{#each subItem in model.array.[key]}}
          <li>Subitem: {{subItem}}</li>

Though the model.array.[key] always seems to be undefined. Is there a trick to associative array lookups in Ember? I would also be happy writing a controller function like getFoodsWithKey(key), but haven't found any resources indicating that calling a controller function (as opposed to a computed property, which to support their caching paradigm, doesn't allow parameters) is possible.

JSFiddle: http://ift.tt/1J7Livi




Aucun commentaire:

Enregistrer un commentaire