My factory is set up with a model that is functioning well, but I am facing difficulty in setting up a function inside it to return a value when called. The issue could be related to syntax or my approach towards it. Here is the code snippet:
.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;
})
The specific challenge lies in the implementation of the getModule() function within the ModuleModelInstance:
function getModule(name){
if (currentModules[name]) {
return true;
} else {
return undefined;
}
}
I intend to call this function in another module where it is injected and use:
moduleModel.getModel(name)
to retrieve either true or undefined. What could possibly be incorrect in my setup? Thank you! Also, note that currentModules is defined in the scope right above for accessibility.
If I display console.log(moduleModule) in the other module where it is injected, I can see the full function 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, I am unable to access it using moduleModel.getModule().