Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular invoke a factory function within the same factory

In my controller, I invoke a factory function that needs to call itself recursively. The code worked when they were just simple javascript functions not defined within the factory, but I am trying to isolate them a bit better.

This is what a snippet of code looks like in the controller:

myApp.controller('VisionCtrl', ['$scope', 'AppFactory', function ($scope, AppFactory,) {
    var promiseTaxo;
    promiseTaxo = AppFactory.getTaxonomy("vision3", "Vision Type");
}])

and in the factory module:

myApp.factory('AppFactory', ['$http', '$q', function($http, $q, TaxoFactory) {
    return {
        getTaxonomy: function(group, termSet) {
            ... lots of code ....
            if (termSet.length > 0) { getTaxonomy(group, childTermSet) }
        }
    }
}])

This is super simplified, but the idea is that in the getTaxonomy function, if I find children node, I recursively call myself. There is a whole lot more stuff going on where I handle the asynch nature of the processing and the promises, but when I put this code outside the factory, it works just fine.

I just don't know how to invoke the getTaxonomy inside the getTaxonomy!

like image 456
pierrebo Avatar asked Mar 20 '23 10:03

pierrebo


1 Answers

You can either call this.getTaxonomy() as @elclanrs mentioned or change up your factory a bit so you can call it from inside:

myApp.factory('AppFactory', ['$http', '$q', function($http, $q, TaxoFactory) {

    var AppFactory = {
        getTaxonomy: function(group, termSet) {
            ... lots of code ....
            if (termSet.length > 0) { AppFactory.getTaxonomy(group, childTermSet) }
        }
    }

    return AppFactory;
}]);
like image 172
Planky Avatar answered Apr 09 '23 22:04

Planky