Support passing the tracker object to $http
JohannesRudolph opened this issue · 9 comments
I was wondering whether we could somehow get rid of using string ids all over the place with promise tracker and use references to the actual tracker objects instead. Like
//Create / get our tracker with unique ID
$scope.loadingTracker = promiseTracker.register();
//use `tracker:` shortcut in $http config to link our http promise to a tracker
$scope.fetchSomething = function(id) {
return $http.get('http://httpbin.org/delay/2', {
tracker: $scope.loadingTracker
}).then(function(response) {
alert('Fetched something!\n' + JSON.stringify(response.data, null, 2));
});
};
Is there any technical reason why we have to use string ids at the moment?
I wanna do this too. We discussed this, check out the last comments on this issue: #37
It would make it even simpler, but it would be backwards incompatible. Which might be OK... things like angular-busy can just keep using angular-promise-tracker 1.x
..and the technical reason is when I made this a year ago I knew a lot less what I was doing, I guess.
I am just dabbling with a fork, because it will definately break the API and is a great opportunity for me to learn how to work with grunt, karma et.al.
The build/dev setup for the project is really sweet, a big kudos!
Ok, I think I'm done removing the ids and the registry. Feel free to take a look.
Looks good! But you can go even further: you don't need .register
and .deregister
.
You can just do var myTracker = new PromiseTracker()
and myTracker.destroy()
.
But the unit tests will be annoying to fix from that :-)
That might be an option. I think that in order to keep the $http.config
integration working, the promiseTracker would need to be injected. Makes
the controllers more tesable as well.
Am 06.02.2014 21:18 schrieb "Andy Joslin" notifications@github.com:
Looks good! But you can go even further: you don't need .register and
.deregister.You can just do var myTracker = new PromiseTracker() and
myTracker.destroy().But the unit tests will be annoying to fix from that :-)
Reply to this email directly or view it on GitHubhttps://github.com//issues/39#issuecomment-34365873
.
Well, if you wanted a global tracker you could put it on rootScope or in the service.
Here is the proposed syntax.
Just-in-a-controller example:
angular.module('myApp', ['promiseTracker'])
.controller('Ctrl', function($scope, PromiseTracker, $http) {
$scope.ctrlTracker = new PromiseTracker();
$scope.fetch = function() {
return $http.get('/stuff', {
tracker: $scope.ctrlTracker
});
};
});
$rootScope example:
angular.module('myApp', ['promiseTracker'])
.run(function(PromiseTracker, $rootScope) {
$rootScope.loadingTracker = new PromiseTracker({ activationDelay: 250, minDuration: 500 });
})
.controller('Ctrl', function($scope, $rootScope, $http) {
$scope.fetch = function() {
return $http.get('/stuff', {
tracker: $rootScope.loadingTracker
});
};
});
Service example:
angular.module('myApp', ['promiseTracker'])
.factory('loadingTracker', function(PromiseTracker) {
return new PromiseTracker({ activationDelay: 250, minDuration: 500 });
})
.controller('Ctrl', function($scope, loadingTracker, $http) {
$scope.fetch = function() {
return $http.get('/stuff', {tracker: loadingTracker});
};
$scope.delay = function() {
var promise = $timeout(function() { alert('delay done!'); }, 1000);
loadingTracker.addPromise(promise);
};
});
What do you think?
/cc @JohannesRudolph @tlvince (tlvince, we discussed this earlier but I still want to do it) @jtammen
Done in 2.0.0.