$http by default returns promise object you dont have to create separate
promise objects, you need to "return $http" like my callapi method. my
service lookslike below
httpservice
define(['application', 'SharedData', 'Variables'], function (app) {
app.register.service('HTTPService', HTTPService);
HTTPService.$inject = ['$http', '$location', 'SharedData', 'config'];
function HTTPService($http, $location, SharedData,config)
{
return {
callAPI: callAPI,
changePage:ChangePage
};
function callAPI(httpMethod, input, routeURL) {
return $http({
method: httpMethod,
data: input,
url: config.apiUrl + routeURL
}).success(httpCallSuccess)
.error(httpCallFailed);
function httpCallSuccess(response) {
//SharedData.setBasket(data);
console.log(response);
return response.data;
}
function httpCallFailed(error) {
console.log(error);
}
}
function ChangePage(Path) {
$location.path(Path)
}
}
});
module service, which is specific to each module call the HTTPservice.
HTTPservice is common to all module services.
function AddItemToBasket(input) {
return HTTPService.callAPI(config.postMethod, input,
config.addItemURL)
.success(function (response) {
return response;
}).error(function(error){
return error;
});
}
My controllers call the respective module services like below, after
obtaining the data it will also change the scope variables of controller.
function Accept() {
var source = {
"clientid": vm.basket.ClientID, "upc": vm.item.upc,
"serialno": vm.item.uii
}
SalesService.addItemToBasket(source).
success(function (data) {
vm.basket = data;
}).error(function (error) {
console.log(error);
});
}
On Monday, 13 July 2015 12:00:26 UTC+5:30, Daniele Ratti wrote:
>
> Hi there, I'm using the code below in order to simplify the backend
> requests but I didn't catch how to call either a success method or an error
> method.
> How can I reach the expected behavior commented in the code?
>
>
>> app.factory('REST', function ($http, $q, sweetAlert) {
>>
>> return {
>> load: function (module, action, data) {
>> var deferred = $q.defer();
>> var promise = deferred.promise;
>> $http
>> .post('/api/'+module+'.php?action='+action, data)
>> .success(function (data) {
>>
>> if(data.error)
>> {
>> sweetAlert.swal({
>> title: "Error",
>> text: data.error,
>> type: "warning"
>> });
>> * //HERE I WANT TO CALL .error(details)*
>> }
>> else
>> deferred.resolve(data.result);
>>
>> }).error(function () {
>> * //HERE I WANT TO CALL .error(details)*
>> });
>> promise.success = function(fn) {
>> promise.then(fn);
>> return promise;
>> }
>> return promise;
>> }
>> };
>>
>> });
>>
>
> This is the code which uses the code above:
>
>
>> $scope.login = function () {
>
> $scope.loading = true;
>
> var payload = {'credentials': $scope.logindata};
>
> REST.load('access', 'login', payload).success(function(data) {
>
> if(data.redirect)
>
> $state.go(data.redirect);
>
> $scope.loading = false;
>
> *}).error(function(data) { //THIS SHOULD BE CALLED*
>
> * $scope.loading = false;*
>
> * });*
>
> }
>
>
>
>
> Thank you very much
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.