Angular utilizes a factory within a model to return specific values

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().

Answer №1

In order to accomplish the task you are inquiring about -

   modify

   function getModule(name){    

   to

   ModuleModelInstance.getModule = function(name){

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to define objects containing arrays of objects in JavaScript?

I am seeking to create a blueprint for the following data structure, with all values provided by the user: { "name":"ABC", "city":"X", "child": [{"name":"ccc","age":"22"}, {"name":"ccc","age":"22"} ] } This data structure consists of a parent obje ...

Prevent inheriting styles with jQuery Else / If conditions

I'm seeking advice on how to prevent elements from retaining the same inline styles in different breakpoints. My goal is to adjust styling based on window width, similar to CSS media queries, but with the additional need to increment a numeric value ...

Tips for combining text and variable outcomes in printing

I created a calculator that is functioning correctly and displaying results. However, I would like to include a text along with the results. For example: You spent "variable result" dollars in the transaction. Currently, the results are shown only after ...

Leveraging JavaScript and PHP for fetching image files and generating a downloadable zip folder

Currently, I am in the process of creating a Safari extension specifically designed for imageboard-style websites. One of the key features that I am eager to incorporate is the ability to download all images that have been posted on the site (not including ...

It is important to ensure that the user returned by the onAuthStateChanged function in

server admin.auth().createCustomToken(uuid) .then((customToken) => { admin.auth().createUser({ email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed989e889fad88958c809d8188c38e8280">[email protected] ...

Establish a specific character limit for input text

Is there a method to restrict the input length of a textbox to exactly 9 characters? I am looking for a solution and any assistance would be greatly valued. Thank you in advance! ...

Uploading files with previews and no option to alter the image dimensions

I am having trouble with resizing an image preview before submitting it. Despite setting the width and height to 1px in both the div and image, it still displays at its normal dimensions. $(function() { var imagesPreview = function(input, placeToIns ...

"Utilize the power of the ajaxform jQuery plugin to automatically reset form fields

Initially, I'd like to emphasize that this is an original inquiry. My predicament is as follows: I have a chatroom php script where I utilize the ajaxForm jQuery plugin to seamlessly send new messages without having to reload the entire page. Howev ...

Guide to building an HTML table on-the-fly using JSON information

I am trying to extract specific data from a "JSON" array. I have attempted the following code, but I am still in need of help. <?php $uri=$_POST['uri']; $api_key=$_POST['api_key']; $board=$uri.$api_key; $value=file_get_contents($boa ...

Delay execution of Selenium WebDriver until the element appears on the screen

After scouring Google and the SO site, I was able to find answers for JAVA but not for node.js. I have a web application that has a slow loading time. I want the selenium program to wait until the page is fully loaded before taking any actions. Below is ...

Can you explain the distinction between cdnjs and npm?

Can you explain the distinctions between cdnjs and npm? Is npm considered a type of CDN (content delivery network)? ...

Encountering an 'Unknown provider' error while running a unit test with AngularJS and Jasmine

I am facing an issue while writing a unit test for a controller in my application. Jasmine is showing an 'Unknown provider' error related to a provider I created for fetching template URLs. This provider is injected into a config function that is ...

Dismiss MUI Snackbar notification and execute action upon pressing a key

A custom notification system has been developed, utilizing the material ui Snackbar with both an action button and a close button. The objective is to implement a listener event for the "enter" key in order to trigger the specific notification's actio ...

Instructions on how to use WebClient in C# to download an image and retrieve it from an ASPX page using JavaScript

I have been searching for solutions to this question everywhere, but none of them seem to work for me. The goal is to download an image from the internet using an aspx page. I want to call the aspx page from JavaScript, retrieve the data, and display it i ...

Leverage the variable from one function in a different function within Three.js using Javascript

After loading an obj file using three.js, I attempted to obtain the 'X' position of its vertices and save it in a variable named 'pos' inside the objloader function within the init() function. My goal was to access this variable's ...

Attempting to connect the dots between JavaScript, HTML, PHP, and refreshing content

As part of a project for a company I recently joined, I am in the process of constructing a CMS. Our approach involves utilizing an MCV structure and my skills with JavaScript, PHP, HTML5, CSS3, and JQuery are instrumental in this endeavor. One key elemen ...

Error encountered in Ubuntu while attempting to run a Python script within a Node.js/Express application: spawn EACCES

Recently, I set up a node.js server to run a python script using the python-shell . However, after migrating from Windows to Ubuntu, an EACCES error has been persistently popping up. Despite my attempts to adjust permissions and troubleshoot, I haven' ...

JavaScript's speciality - altering the Jquery fade in and fade out effect on Firefox browsers

Utilizing Jquery, I implemented a feature on a table where moving the mouse over a row changes its color. The Javascript code was specifically designed for IE7 and works flawlessly there. However, when tested in Firefox, the text fades along with the backg ...

"What is the best way to access the value of a useState variable in ReactJS on a global

Below is my reactJS code snippet - useEffect(()=>{ const getinterviewerDetails= async ()=>{ const data1 = await axios.get("http://localhost:8083/api/GetProduct") .then(response => { console.log("role is " ...

Exploring the power of makeStyles in Material UI when combined with TypeScript

I am currently in the process of converting a JavaScript template to Typescript. Here is an example of my accordionStyle.ts file: import { primaryColor, grayColor } from "../../material-dashboard-pro-react"; const accordionStyle = (theme?:an ...