R
R
RodgerFox2016-01-12 04:53:16
Angular
RodgerFox, 2016-01-12 04:53:16

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;
                });
        }

factory:
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);
            }
        }

And what if I use $resource???
In the loadProfileResource() function, when calling ProfileFactory.getProfile(), there is an error, TypeError: ProfileFactory.getProfile.query is not a function
ProfileFactory.getProfile.query() - it will also not be correct
Please tell me the solution.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kano, 2016-01-12
@RodgerFox

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();
}

R
RodgerFox, 2016-01-12
@RodgerFox

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;
                });
}

Here is the complete factory code:
(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: {
                  //и список других методов
                }
            });
        }
    }
})();

The factory works with a part of the application, isp. $resource and in the future there will be more functions and method options, can't it be systematized like that?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question