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) {
Ember.$.ajax({
type: 'GET',
url: apiRoot + path,
dataType: 'json',
data: query
}).then(function(data) {
Ember.run(null, resolve, data);
}, function(jqXHR) {
jqXHR.then = null; // tame jQuery's ill mannered promises
Ember.run(null, 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) {
this.store.pushPayload("user", data);
}, function(jqXHR) {
console.log(jqXHR);
alert("Error!");
});
}
}
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; ... that.store(...
or should I instead bind the context already in the getJSON method, by doing Ember.run.bind(this, resolve, data);
or something entirely different?
EDIT: Oops the second idea does not actually work, how could this be achieved?