MagicMirror² v2.5.0 is available! For more information about this release, check out this topic.

How to pass a value between functions in a Module?



  • Hi,

    I’m trying to pass the value in the variable ajaxResult to the getDom function so that I can render it into a table.

    I’ve tried to return the value from the start function but that comes up as undefined

    How are values passed between these functions?

    Thanks for the help in advance

    `/* global Module */

    Module.register(“trainScheduler”,{

    // Default module config.
    defaults: {
    	text: "| --- | --- | --- | --- |"
    },
    
    currentWeatherType: "",
    
    // Define required scripts.
    getScripts: function() {
    	return ["moment.js"];
    },
    
    // Define start sequence.
    start: function() {
    	Log.info("Starting module: " + this.name);
    
    	var ajaxResult; 
    
    	// Log.info('RESULT' + ajaxResult);
    
    	if (this.config.remoteFile != null) {			
    		this.trainSchedule(
    			(response) => {				
    				this.config = JSON.parse(response);
    				//function call to send the result to out!
    				responseRunner(response);
    				
    			}
    		);
    		function responseRunner (response) {
    			ajaxResult = response;
    		};
    
    	}
    
    	// Schedule update timer.
    
    	// LEMME COME BACK TO THIS AUTO REFERESHER THING
    
    	// var self = this;
    	// setInterval(function() {
    	// 	self.updateDom(self.config.fadeSpeed);
    	// }, this.config.updateInterval);
    },
    
    /* complimentFile(callback)
     * Retrieve a file from the local filesystem
     */
    trainSchedule: function(callback) {
    	var xobj = new XMLHttpRequest();
    	xobj.overrideMimeType("application/json");
    	xobj.open("GET", this.file(this.config.remoteFile), true);
    	xobj.onreadystatechange = function() {
    		if (xobj.readyState == 4 && xobj.status == "200") {
    			callback(xobj.responseText);
    		}
    	};
    	xobj.send(null);
    	
    },
    
    // Override dom generator.
    getDom: function() {
    
    	var complimentText = 1;
    	var compliment = document.createTextNode(complimentText);
    	var wrapper = document.createElement("div");
    	wrapper.className = this.config.classes ? this.config.classes : "thin xlarge bright";
    	wrapper.appendChild(compliment);
    
    	return wrapper;
    },
    

    });
    `



  • @ameenaziz Have a look at how the default modules do it, like newsfeed. It creates a this.newsItems variable, but also a this.loaded boolean. It uses socketNotificationRecieved() to trigger generateFeed() which updates this.newsItems, then changes this.loaded to true.



  • @ninjabreadman thanks for the guidance, the new modules is pretty complex, is there a simpler module to take an example from?

    All I’m really after is the data produced from the trainSchedule function to be accessible by the getDom function.



  • @ameenaziz I think if you change var ajaxResult; to this.ajaxResult = ""; and change all other references (from ajaxResult to this.ajaxResult), you should be able to access it from anywhere in your module, including getDom(). Just remember when getDom() is first called, this.ajaxResult may still be empty until the response is received.


  • Module Developer

    getDom: function() is called initially to set up the content. Usually you have something like div.innerHTML = variable; but at the beginning, varibable might be empty. To make the variable usable globally (in the scope of your module), you set it up and call it as this.variable.

    When your function that fills this variable updates that variable content, you call updateDom();
    That makes the dom reload and this time, this.variable has content to show.

    There’s other ways to fill the element with the content of a variable without totally rebuilding the DOM:
    In the function that handles the variable, you could say document.getElementbyId(elementId).innerHTML = variable;

    Edit: To complete this. If you want to pass a value from one function to another (but this is not working for the Dom, because that has to be set up initially):

    firstFunction: function () {
        var greeting: "Hello";
        secondFunction(greeting);
    }
    secondFunction: function (word) {
        var greeting: word;
        Log.info(greeting); // "Hello"
    }