EasyCo
EasyCo

Reputation: 2036

Accessing previously loaded data in EmberJS

In the Ember JS Application Guide, the author shows how to show a list of shoes and then eventually just show one shoe.

The problem, is in his example, he mimics a slow JSON request (using setTimeout) to get ALL shoes but then he just refers directly to the object literal to show one shoe.

My question: What is the appropriate approach to pull data using JSON once for a collection of items, then refer to the downloaded collection for other methods (i.e Show one shoe)

I don't need it to be persistent across sessions, just work until the user closes the browser/tab.

The JSFiddle

Upvotes: 2

Views: 80

Answers (1)

McGarnagle
McGarnagle

Reputation: 102743

In the example, the setTimeout callback places the collection into App.Shoe, which is a global object. You can access it any time after that using App.Shoe._listOfShoes. So, if you just want to load the collection once and then re-use it, you could add confirm before making the request, whether the data has already been loaded. That is, something like:

if (this._listOfShoes.length == 0) {
    // load the collection
}
return this._listOfShoes;

Upvotes: 1

Related Questions