2

New to AngularJS and I guess I don't understand how to call one Promise method from another with the same factory. Every time my code gets to the $http.get within processPerson, I get a Function Expected error in IE, or an Object is not a Function error in Chrome. I've tried reorganizing code many times, multiple factories, etc, and generally get the same error. The only time I can get this to work is if I combine the functions where the processPerson function is embedded within the success of the getPersonnel.

Code:

(function(){

var app = angular.module('hrSite', ['personnel']);

app.controller('PersonnelController', function($scope, personnelFactory){
    var personnelPromise = personnelFactory.getPersonnel();
    personnelPromise.then(function(personnel){
        var perDefs = new Array();
        $.each(personnel.data.value, function( i, person ){
            var perDef = personnelFactory.processPerson(person);
            perDefs.push(perDef);
        });
        $q.all(perDefs).then(function(){
            $scope.personnel = personnel.data.value;
        });
    });
});

})();

(function(){

var personnelModule = angular.module('personnel', []);

personnelModule.factory('personnelFactory', function($http, $q) {

        var getPersonnel = function(){
            return $http.get("/sites/Development/_api/web/lists/getbytitle('Personnel')/items");
        };

        var processPerson = function(person){
            var deferred = $q.defer();
                $http.get("/sites/Development/_api/web/lists/getbytitle('Personnel Skills')/items?$select=*,Skill/Id,Skill/Title&$filter=PersonId eq '"+person.Id+"'&$expand=Skill").then(function(skills){
                    person.Skills = skills.data.value;
                    person.SkillsId = [];
                    $.each(skills.data.value, function( j, skill ){
                        person.SkillsId.push(skill.Id);
                    });
                    deferred.resolve();
                });
            return deferred.promise();
        };
        return {getPersonnel: getPersonnel,
                processPerson: processPerson}
});
})();
4

1 Answer 1

4

Nevermind - I figured it out. I was migrating code from a jQuery project and in jQuery, you return a promise like this:

return deferred.promise();

Since Angular has its own deferred feature, $q, I began using that, without realizing that the notation to return a promise was slightly different:

return deferred.promise;

No () in that, which was really screwing things up. Now everything seems to be working fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.