Ember.js: The correct way to enable Em. $. GetJSON in promise and binding response to controller context?

I get some data with $ .getJSON that I want to bind asynchronously to the controller context. I came up with this on my route - it works, but I'm not happy with this:

setupController: function(controller, model) { this._super(controller, model); Em.RSVP.Promise.cast(Em.$.getJSON((this.get('ENV.apiBaseURL')) + "/users/current/live_matchday_stats")).then((function(_this) { return function(s) { return _this.controller.set('matchdayStats', Em.Object.create(s)); }; } 

Then in my template, I can, for example, use:

 Foo: {{matchdayStats.foo}} 

And everything works fine. Is there a better way to write this (possibly without promises and creating Em.Object). I know this automatically works if I embed Em. $. GetJSON in the hook of the model.

+7
javascript
source share
1 answer

You can use DS.PromiseObject

 var matchdayStats = DS.PromiseObject.create({ promise: Em.$.getJSON((this.get('ENV.apiBaseURL')) + "/users/current/live_matchday_stats") }); controller.set('matchdayStats', matchdayStats); 

This is the way that Ember Data accesses / displays related objects and the properties of these objects in templates.

+1
source share

All Articles