Jiew Meng
Jiew Meng

Reputation: 88207

Handling async model data in EmberJS 1.0.0

I am using EmberJS v1.0.0-pre.4 for a project at work ... old yes ... but since its for work ... and I have problems upgrading (not sure if the company will want to also), I guess I have to find a workaround.

I just want to use load data for my model asynchronously ... I believe with latest version, I can return a promise for my model ...

App.XRoute = Ember.Route.extend({
    model: function() {
        return $.getJSON(...);
    }
});

But with the old version, it appears this does not work ... so how should I handle async data?

Upvotes: 1

Views: 67

Answers (2)

Kingpin2k
Kingpin2k

Reputation: 47367

Too long for comment, I'll gladly keep working with you on this, but it appears to work 1.0 pre 4

 App = Ember.Application.create();

 App.Router.map(function () {
     // put your routes here
 });

 App.IndexRoute = Ember.Route.extend({
    model: function () {
      return new Promise(function (resolve) {
        setTimeout(function () {
          resolve(['red', 'yellow', 'blue']);
        }, 2000); 
      });
    }
 });

Upvotes: 1

Karl-Johan Sjögren
Karl-Johan Sjögren

Reputation: 17532

There is something with the jQuery promise-implementation that doesn't work that well with Ember, I can't for my life remember what it was now though..

There is however a great wrapper that you can use for this called ic-ajax. It supports the same syntax as jQuery.ajax but wraps it in a promise using the RSVP-library that Ember uses. It also only add about 4kb unminimized so it isn't going to make your project size skyrocket.

I've been using in two Ember-projects so far and now it's included with ember-cli as well (I'm in no way affiliated with the ic-ajax project or it's developers, I just think it's great).

Upvotes: 1

Related Questions