Answer the question
In order to leave comments, you need to log in
How to overcome nesting with calls?
Hello everyone, there is a controller with a factory connected to it, in the controller:
loadProfile();
function loadProfile() {
return loadProfileResource().then(function() {
console.log('loadProfileResource Activated!');
});
}
function loadProfileResource() {
return ProfileFactory.getProfile.query()
.then(function(data) {
$scope.profile = angular.copy(data);
return $scope.profile;
});
}
return {
getActiveProfile: getActiveProfile,
getProfile: getProfile
};
function getActiveProfile() {...}
function getProfile() {
var profile = $resource('/loadProfile&token='+$rootScope.token, {id_profile: '@id_profile'})
return profile;
/*
с исп $http все просто и понятно и это работает
return $http.get('/loadProfile&token='+$rootScope.token)
.then(getActiveProfileComplete)
.catch(getActiveProfileFailed);
*/
function getActiveProfileComplete(response) {
console.log(response);
return response.data;
}
function getActiveProfileFailed(error) {
console.log('XHR Failed for getActiveProfile.' + error.data);
}
}
Answer the question
In order to leave comments, you need to log in
Error here - ProfileFactory.getProfile.query()
It is necessary - ProfileFactory.getProfile().query()
And it's better like this
function getProfile(token) {
var profile = $resource('/loadProfile', {token:token, id_profile: '@id_profile'});
return profile.query();
}
In the controller, the factory call function:
function loadProfileResource() {
return ProfileFactory.getProfile(token).test({id_profile: 158}).then(function(data) {
// или вариант
// return ProfileFactory.getProfile(token).get().then(function(data) {
// .query(), update и т.д.
$scope.profile = angular.copy(data);
console.log('+++');
return $scope.profile;
});
}
(function() {
'use strict';
angular
.module('LenaPillars')
.factory('ProfileFactory', ProfileFactory);
ProfileFactory.$injector = ['$rootScope', '$resource', '$http', 'logger'];
function ProfileFactory($rootScope, $resource, $http, logger) {
return {
getActiveProfile: getActiveProfile,
getProfile: getProfile
};
function getActiveProfile() {
return $http.get($rootScope.storeUrl+'/checkProfile&token='+$rootScope.token)
.then(getActiveProfileComplete)
.catch(getActiveProfileFailed);
function getActiveProfileComplete(response) {
console.log(response);
return response.data;
}
function getActiveProfileFailed(error) {
console.log('XHR Failed for getActiveProfile.' + error.data);
}
}
function getProfile(token) {
return $resource($rootScope.storeUrl+'/loadProfile&token='+$rootScope.token, {id_profile: '@id_profile'}, {
test: {
method: 'GET'
},
variant: {
//и список других методов
}
});
}
}
})();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question