I would like to access my route's controller from within the beforeSend hook on a route to take advantage of the pause on promise logic.
This is my current workaround to be able to set "category_config" on my controller which is obtained from a promise in beforeModel.
Imaging.ReferenceRoute = Ember.Route.extend(Imaging.Ajax, {
setupController: function(controller, model) {
controller.set('error_messages', []);
controller.set('category_config', this.get('category_config'));
return this._super(controller, model);
},
beforeModel: function(transition) {
var categories;
categories = this._promise("/url/foo/", "GET");
return Ember.RSVP.all([categories]).then(((function(_this) {
return function(response) {
return _this.set('category_config', response[0]);
};
})(this)));
},
model: function() {
return Imaging.Document.find();
}
});
In case you are curious my _promise helper is below:
_promise: function(url, type, hash) {
return new Ember.RSVP.Promise(function(resolve, reject) {
hash = hash || {};
hash.url = url;
hash.type = type;
hash.dataType = "json";
hash.success = function(json) {
return Ember.run(null, resolve, json);
};
hash.error = function(json) {
if (json && json.then) {
json.then = null;
}
return Ember.run(null, reject, json);
};
return $.ajax(hash);
});
}
How can I do this without having the beforeModel set the 'category_config' on the route, and then set it on the controller in setupController?
Not tested, but I think you could return a tuple with the Imaging.Document.find()
and the _promise("/url/foo/", "GET");
, using Ember.RSVP.hash
Imaging.ReferenceRoute = Ember.Route.extend(Imaging.Ajax, {
setupController: function(controller, model) {
controller.set('error_messages', []);
controller.set('category_config', model.category_config);
return this._super(controller, model.document);
},
model: function() {
return Ember.RSVP.hash({
document: Imaging.Document.find(),
category_config: this._promise("/url/foo/", "GET")
});
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With