Should I use binding or lexical closure with

I have this functions, that I’ve adapted from some sample code.

var getJSON = function(path, query) {
  return new Ember.RSVP.Promise(function(resolve, reject) {
      type: 'GET',
      url: apiRoot + path,
      dataType: 'json',
      data: query
    }).then(function(data) {, resolve, data);
    }, function(jqXHR) {
      jqXHR.then = null; // tame jQuery's ill mannered promises, reject, jqXHR);

And I call it from a route:

events: {
  login: function () {
    var model = this.modelFor('sessions/login'); // <App.LoginCreds>
    this.getJSON('/authuser', model.serialize()).then(function(data) {"user", data);
    }, function(jqXHR) {

And this results in → TypeError: Cannot read property 'store' of undefined

What’s the (coining the rails term) ember way of solving this? Should I use just regular lexical closure var that = this; ... or should I instead bind the context already in the getJSON method, by doing, resolve, data); or something entirely different?

EDIT: Oops the second idea does not actually work, how could this be achieved?

Yes, exactly that. Within the .then() callback you lost the scope, so store the value of this outside of it in a separate variable and use it there.

Incidentally, if you’re using Ember CLI and you’re up to date, you can now do this:

this.getJSON('/authuser', model.serialize()).then( (data) => {"user", data);
}, ...

Thank’s guys. @totallymike that’s so cool. Exactly something I was looking for! :smile:

Um, I need some help. My understanding of is that “the invocation of that method be safely handled and executed by the Ember run loop.” With the comments on this it seems we are attempting to do the same thing that does but manually in two different steps! Please correct me if I’m wrong but wouldn’t using to return the call back function eliminate the need for var _this = this; or () => {} and allow the function definition to be part of the event object instead of an inline anonymous function (which are notoriously hard to read)?

Sorry to revive an old thread but I just can’t wrap my head around the idea that doesn’t work as expected.