blueFast
blueFast

Reputation: 44381

Caching and walking an ArrayController

I have a Phone model. I want to cache all phones on my application:

cachePhones : function () {
    this.set('phones', this.Phone.find());
},

And get the countries for which I have available phones:

getCountries : function () {
    var phones = this.get('phones.content');
    var countries = new this.dataSet(), country;
    console.log('phones=%o', phones.length);
    for (var index = 0, length = phones.length; index < length; index++) {
        country = phones[index].record.get('country');
        console.log('Adding %o', country);
        countries.add(country, true);
    }
    console.log('countries=%o', countries.keys());
    return countries.keys();
},

(dataSet is just a set implementation in javascript)

I am not sure this is the right way to walk the ArrayController:

This feels like hacking around my way in the ember internals. I have tried before this:

var phones = this.get('phones');
var countries = new this.dataSet(), country;
for (var index = 0, length = phones.length; index < length; index++) {
    country = phones[index].country;
    countries.add(country, true);
}

But it was not working at all. What is the canonical way of walking an ArrayController?

Upvotes: 0

Views: 53

Answers (2)

intuitivepixel
intuitivepixel

Reputation: 23322

Besides @mavilein correct answer one thing worth mentioning is that if you have a model like App.Phone then after you do App.Phone.find() and the records are fetched, your Store has already a cache which you can consult with App.Phone.all() this will not make another request but gives you the records available in the Store.

Hope it helps.

Upvotes: 2

mavilein
mavilein

Reputation: 11668

Have you tried something like this? Normally you should always be fine with using the functional methods Ember offers for its collections.

var phones = this.get('phones');
var countries = new this.dataSet(), country;
phones.forEach(function(phone, index){
  country = phone.get("country");
  countries.add(country, true);
});

Upvotes: 2

Related Questions