I'm trying to use Angular promise API in my app. And I was a little confused. I created a factory like in code below
factory('transport', function ($resource) {
var baseUrl = "http://aw353/WebServer/odata/Payments";
return $resource("", {},
{
'getAll': { method: "GET",params: {svcUrl:"@svcUrl"}, url: baseUrl + ":svcUrl" },
'save': { method: "POST", params: {svcUrl:"@svcUrl"}, url: baseUrl+ "(:svcUrl)" },
'update': { method: 'PUT', params: {svcUrl:"@svcUrl", key: "@key" }, url: baseUrl+ "(:svcUrl)" + "(:key)"},
'query': { method: 'GET', params: {svcUrl:"@svcUrl", key: "@key" }, url: baseUrl +"(:svcUrl)" + "(:key)"},
'remove': { method: 'DELETE', params: {svcUrl:"@svcUrl", key: "@key" }, url: baseUrl + "(:svcUrl)" + "(:key)"}
});
});
I'm using this factory in controller and when I implement the function like this
var getData = function () {
(transport()).$getAll({svcUrl:"//BasicSettings"})
.then(function (data) {
$scope.DataSource = data.value[0];
console.log($scope.DataSource.SystemName);
});
}();
it fails and I get error "Cannot read property '$getAll' of undefined"
But when I use 'new' keyword like this
var getData = function () {
(new transport()).$getAll({svcUrl:"//BasicSettings"})
.then(function (data) {
$scope.DataSource = data.value[0];
console.log($scope.DataSource.SystemName);
});
}();
it works.
I know the difference between constructor function and usual function. But I don't understand why promise API works only in second case.
Can anyone help me to understand how it works?