• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

How to pass a value between functions in a Module?

Scheduled Pinned Locked Moved Development
5 Posts 3 Posters 1.9k Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    ameenaziz
    last edited by Feb 22, 2018, 1:11 AM

    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;
    },
    

    });
    `

    N 1 Reply Last reply Feb 22, 2018, 1:29 PM Reply Quote 0
    • N Offline
      ninjabreadman @ameenaziz
      last edited by Feb 22, 2018, 1:29 PM

      @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.

      Problem with config or JavaScript? Copy/paste it into JSHint.
      Check out the detailed walkthroughs on install, config, modules, etc.

      A 1 Reply Last reply Feb 22, 2018, 11:02 PM Reply Quote 0
      • A Offline
        ameenaziz @ninjabreadman
        last edited by Feb 22, 2018, 11:02 PM

        @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.

        N 1 Reply Last reply Feb 22, 2018, 11:15 PM Reply Quote 0
        • N Offline
          ninjabreadman @ameenaziz
          last edited by Feb 22, 2018, 11:15 PM

          @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.

          Problem with config or JavaScript? Copy/paste it into JSHint.
          Check out the detailed walkthroughs on install, config, modules, etc.

          1 Reply Last reply Reply Quote 1
          • D Offline
            doubleT Module Developer
            last edited by doubleT Feb 23, 2018, 8:41 PM Feb 23, 2018, 8:38 PM

            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"
            }
            1 Reply Last reply Reply Quote 0
            • 1 / 1
            1 / 1
            • First post
              1/5
              Last post
            Enjoying MagicMirror? Please consider a donation!
            MagicMirror created by Michael Teeuw.
            Forum managed by Sam, technical setup by Karsten.
            This forum is using NodeBB as its core | Contributors
            Contact | Privacy Policy