R
R
RodgerFox2015-11-29 03:17:12
Angular
RodgerFox, 2015-11-29 03:17:12

What is the correct way to call a function in a factory?

Hello everyone, tell me how to get rid of this terrible error. there is a directive, its task is to display the object received from the request to the factory, which in turn receives information from $resource.

// директива должна вывести инфу из запроса от фабрики ProfileResource
(function() {
'use strict';
angular
    .module('LenaPillars')
    .directive('statusProfileDirective', statusProfileDirective);

    statusProfileDirective.$injector = ['$log', 'ProfileResource'];

    function statusProfileDirective($log, ProfileResource) {        
        return {
            // controller: 'MainController',
            restrict: 'E',
            templateUrl: 'templates/directive/checkProfileService.html',
            replace: true,
            transclude: true,
            scope: {
                status: '&'
            },
            link: function ( scope, element, attrs) {
                scope.statusus = getProfile();
                console.log(scope.statusus);
            }

        }

        function getProfile() {
            return getProfileCheck().then(function() {
                logger.info('Все прошло норм');
            });
        }

        function getProfileCheck() {
            return ProfileResource.then(function() {
                return checkProfile.checkActiveProfile()
                .then(function(data) {
                    console.log('gde ti moi data: '+data);
                    vm.avengers = data;
                    return vm.avengers;
                });
            });
        }
        
    }
})();

Here is the main catch in this madness:
return ProfileResource.then(function() {
                return checkProfile.checkActiveProfile()

Actually she should be
ProfileResource.checkProfile.the(function() { ..... 
//но как в ней обратиться к checkActiveProfile ??? ужасно туплю, понимаю что то переставить и все должно работать

factory:
// ProfileResource фабрика с $resourse
(function() {
'use strict';
angular
    .module('LenaPillars')
    .factory('ProfileResource', ProfileResource);

    ProfileResource.$injector = ['$rootScope', '$resource'];

    function ProfileResource($rootScope, $resource) {
        return {
            checkProfile: checkProfile
        }
        function checkProfile() {
            return $resource($rootScope.storeUrl+'/checkProfile&token='+$rootScope.token, 
                {
                    setting_profiles: 'setting_profiles'
                }, 
                {
                    'checkActiveProfile': {
                        method: 'GET', isArray: false
                    }
                })
            .then(getAvengersComplete)
            .catch(getAvengersFailed);
        }
        function getAvengersComplete(response) {
            return response.data.results;
        }

        function getAvengersFailed(error) {
            logger.error('XHR Failed for getAvengers.' + error.data);
        }
    }
})();

lena_pillars/templates/directive/checkProfileService.htm simple {{status}}, all content will dance from it.
And yet, Here is a directive, with the output of information from the factory, it is executed once, but how to force it to re-apply from the controller (s) which (s) can be insanely far from its location in the structure?
Thanks a lot.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2015-11-29
@healqq

You have some meat in general, to be honest : (
Usually separate the configuration for $resource and response processing into different parts of the code.
In your case, I would do this:

(function() {
'use strict';
  angular
    .module('LenaPillars')
    .factory('ProfileResource', ProfileResource);

    ProfileResource.$injector = ['$rootScope', '$resource'];
    function ProfileResource($rootScope, $resource) {
    var photosResource = $resource(
      $rootScope.storeUrl, 
      {
        checkActiveProfile: {
                method: 'GET', 
                isArray: false,
                url: $rootScope.storeUrl+'/checkProfile'
            }
      }
      
    );

    function checkActiveProfile() {
      return new photosResource()
        .$checkActiveProfile({token: $rootScope.token, setting_profiles: 'setting_profiles'})
// then принимает 2 параметра, для resolve и reject
        .then(getAvengersComplete, getAvengersFailed);
      function getAvengersComplete(response) {
            return response.data.results;
        }

        function getAvengersFailed(error) {
            logger.error('XHR Failed for getAvengers.' + error.data);
        }
    }

    return {
      checkActiveProfile: checkActiveProfile,
    }
  }
})();

Calling a function anywhere would look like this:
function getProfileCheck() {
    return ProfileResource
    .checkActiveProfile()
    .then(function(data) {
        console.log('gde ti moi data: '+data);
        vm.avengers = data;
        return vm.avengers;
    });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question