I really like the clean (and I think easy to follow) way that promises were autounwrapped:
$scope.myData = DataService.query({something:"etc"}); // done;
And I really don't care for what seems to be the standard way of doing it now, without the automatic unwrapping:
DataService.query({something:"etc"}).$promise.then(function (data){
$scope.myData = data;
});
What I'd like to see is something like this:
$scope.pulseData = $scope.setPromise(CitsciAnalytics.pulse({
projId:"yardmap"
}));
But I can't see how to make that happen. The closest I got was:
$scope.pulseData = $scope.setPromise("pulseData", CitsciAnalytics.pulse({
projId:"yardmap"
}));
Using a function added to the root scope:
.run(["$rootScope", "$log", function ($rootScope, $log) {
//parent method to avoid promise unwrapping boilerplate
$rootScope.setPromise = function (scopeVar, promise) {
if (arguments.length === 2 && promise && promise.$promise) {
var scope = this;
promise.$promise.then(function (data){
scope[scopeVar] = data;
});
} else {
$log.error("$rootScope.setPromise has invalid arguments");
}
};
}]);
but I don't like the unDRY requirement of having to pass the scope variable name as an additional string. Has anyone else tackled this, or see a way to do this more cleanly?