Last active
August 29, 2015 14:22
-
-
Save timestep/eb04628c5268cb64b1fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//to use call getOrders.sum().then(function(sum){console.log(sum)}); | |
angular.service('getOrders',function ($http,$q) { | |
return { | |
sum: function () { | |
var d = $q.defer(); | |
//can add try method here | |
$http.get('/orders').success(function (data) { | |
//assuming data is just json array of orders. | |
var orders = data; | |
var sumOfOrders = 0; | |
orders.forEach(function (order) { | |
sumOfOrders += order.value; | |
}); | |
return d.resolve(sumOfOrders); | |
}).error(function (res) { | |
return d.reject(res); | |
}); | |
return d.promise; | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment