ajmajmajma
ajmajmajma

Reputation: 14216

Angular, function in a (model) factory to return values

I have a factory I am using for a model that is working great, I am trying to set up a function inside of it to return a value when called, and can't seem to get it quite correct. Possibly syntax, or the way I am thinking about it is incorrect. Here is what I have

.factory('moduleModel', function($rootScope, stateModel) {
    function ModuleModelInstance(name, callback) {

        if (currentModules[name]) {
            $log.warn(name + " module already exists.");
        } else {
            this.currentState = {};
            this.callback = callback;
            this.rootScope = $rootScope.$id;
            _.extend(currentModules, _.object([
                [name, this]
            ]));
        }

        function getModule(name) {
            if (currentModules[name]) {
                return true;
            } else {
                return undefined;
            }

        }
    };

    ModuleModelInstance.prototype = {
        //add New State to module
        addState: function(state, stateVars) {
            this.states = new stateModel(state, stateVars);
        },
        setCurrent: function(state, stateVars) {
            if (this.states.canGo(state, stateVars)) {
                this.currentState = _.object([
                    [state, stateVars]
                ]);
                return true;
            } else {
                return false;
            }
        }
    };

    return ModuleModelInstance;
})

Now where I am having trouble is with that function inside the ModuleModelInstance, the getModule() -

function getModule(name){
    if (currentModules[name]) {
        return true;
    } else {
        return undefined;
    }
}

I want to be able to call it in another module where this is injected and do something like

moduleModel.getModel(name)

and have it pass back either true or undefined. What am I doing wrong here? Thanks! Also - currentModules is defined in the scope right above it so it has access too it here (just to clarify).

If I console.log(moduleModule) in the other module where it is injected, I can see the full function in the console like -

   function ModuleModelInstance(name, callback) {

            if (currentModules[name]) {
                $log.warn(name + " module already exists.");
            } else {
                this.currentState = {};
                this.callback = callback;
                this.rootScope = $rootScope.$id;
                _.extend(currentModules, _.object([
                    [name, this]
                ]));
            }

            function getModule(name){
                if(currentModules[name]){
                    return true;
                }else{
                    return undefined;
                }
            }
        }

However cannot seem to access it with moduleModel.getModule() .

Upvotes: 0

Views: 109

Answers (1)

user2815588
user2815588

Reputation:

To do what you are asking -

   change

   function getModule(name){    

   to

   ModuleModelInstance.getModule = function(name){

Upvotes: 1

Related Questions