Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with Angular's $http/$q services #37

Open
lsd opened this issue Jan 6, 2015 · 4 comments
Open

Compatibility with Angular's $http/$q services #37

lsd opened this issue Jan 6, 2015 · 4 comments

Comments

@lsd
Copy link

lsd commented Jan 6, 2015

I'm wondering if anyone has attempted to get angular's native $http service to work as the request lib Hyperagent uses?

It seems straightforward to decorate $http accordingly but I haven't tried it myself yet because I'm in a project where we've accepted jQuery.ajax long ago.

The important issue here is making Angular aware of requests HA is making so as to work seamlessly with Angular's internals (interceptors and so forth)

@giosh94mhz
Copy link

I didn't tried, but 👍 . Using $scope.apply every time become a tedious work, and obfuscate the code logic.

@passy
Copy link
Contributor

passy commented Feb 8, 2015

There's definitely room for a simple wrapper, but that's out of scope for this library. Happy to link to an Angular plugin/wrapper/extension from here if someone wants to take that on.

@sdbondi
Copy link

sdbondi commented Mar 5, 2015

If you are interested, here is my wrapper function for $http.

    angular.module('my.app', []).run ($http) ->
            HyperAgent.configure 'ajax', (options) ->
                # Ensure success/error callbacks are called as HyperAgent relies
                # on these to resolve its promise                           
                $http(options)
                    .then (resp) -> options.success(resp.data)
                    .catch(options.error)

$q is easier;

    HyperAgent.configure('defer', $q.defer)

@jfexyz
Copy link

jfexyz commented Apr 14, 2015

In order to get angular to play nice with Hyperagent, you need to make sure angular doesn't parse the result. angular doesn't respect the dataType setting, so I needed to do this:

angular.module('app', []).run(['$http', '$q', function ($http, $q) {

  Hyperagent.configure('defer', $q.defer);

  Hyperagent.configure('ajax', function (options) {
    options.transformResponse = function (data) {
      return data;
    };
    return $http(options)
      .success(options.success)
      .catch(options.error);
  });

}]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants