jeudi 26 février 2015

Global AJAX Error Handler in ember.js

I'm building an Ember application that has a lot of integration with a back-end API, so I'd like to set up a global AJAX error handler to display to the user if AJAX requests fail. How do I set up a global AJAX error handler in ember.js?


So far, I have an errorController:



MyApplication.ErrorController = Ember.ObjectController.extend({
errorTitle:"",
errorDescription:"",
actions: {
backToHome: function() {
this.transitionToRoute('home');
}
}
});


And I'm trying to use jQuery's ajaxError function to capture an AJAX failure:



Ember.$(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) {
var errorController = MyApplication.controllerFor('error');
errorController.set('errorHeading', 'AJAX error');
errorController.set('errorDescription', jqXHR.responseText); //server will return the error description
MyApplication.transitionToRoute('error');
});


However, controllerFor doesn't exist in the MyApplication variable. I'm not sure what other way to access ErrorController since the error function is completely out of the scope of the Ember application.





Aucun commentaire:

Enregistrer un commentaire