How do you intercept all HTTP requests in an EmberJs application?

I would like to be able to capture all HTTP requests and responses and modify them before they move on to the rest of the EmberJs application. I would like to do it globally - throughout the application. I could not find this digging through the API . How can I do that?

(Modification - execute some conditional logic based on certain headers, or add or change certain headers).


In AngularJS, you can accomplish this using something like this:

App.factory('AppHttpInterceptor', function($q) {
    return {
        request: function(req) {
            //modify request
            return req;
        },
        response: function(res) {
            // modify response
            return res || $q.when(res);
        }
    };
});

App.config(function ($httpProvider) {
  $httpProvider.interceptors.push('AppHttpInterceptor');
});
+4
source share
1 answer

-, , Angular Ember . - javascript, , . Angular promises . , (. ).

AngularJS $httpProvider , $http . Angular , ( , ), , Angular , Ember, , .

, Ember, . - cerebris/ember-rest , , . Ember.resource, :

Ember.ResourceAdapter.extend({
    _prepareResourceRequest: function(params) {
      params.beforeSend = function (xhr, settings) {
       //set your xhr interceptors here
      }
    }
  });

: $ajax ember $http Angular (promises )

, Angular , , Angular promises, $ajax . , , / , $ajax , Promises , , , .

, - $ajaxPrefilter, , $ajax. Ember , AngularJS $http, /, , xhr- , $ajax.

jQuery $ajaxSetup(), dataFilter , . angular, $httpProvider , - , , http . ajax jQuery , .

, , ng-conf 2014: Christion Lilley: Going Postal Angular Promises

2: Ember.RSVP

Ember.RSVP , , . , HTTP- RSVP.deferred , HTTP .

, . RSVP.

+6

All Articles