Global variables in Meteor

  • 3 Answer(s)

    Define a variable in the classic JavaScript:

    var someVar = 'someValue';

    The root of .js file Meteor scopes it to the file using an IIFE.

    Need to define a global variable, simply not write the var, giving :

    someVar = 'someValue';

    And define a variable in your application by default, you may restrict it by writing that declaration in a specific recognized folder for example client or server folder.

    This variable not be defined first. It can be defined at Meteor runs the actual code that defines it. it may not be the best practice because you are going to struggle with load order, and it will make your code dependent on Meteor loads files.

    Answered on December 24, 2018.
    Add Comment

    Just you can use this solution,

        The Variables in Meteor declared with the var keyword are scoped to the file and they are declared in. Then need  to create a global variable to try the below code:

    Schemas = {}
    Answered on December 24, 2018.
    Add Comment

    At the ReferenceError is a Node error. Meteor is a framework on top of Node:

    It is a Node has a global scope global variable. This error is thrown by Node and not Meteor, if try to access an undefined global variable.

    The Browsers also have a global scope called window, and do not throw ReferenceErrors and undefined variables are accessed.

    Here a pattern for adding functionality to a class it’s Meteor:

    /lib/Helpers.js <-- Helpers for everyone (node+browser)
    /server/Helpers.js <-- Server helpers (node)
    /client/Helpers.js <-- Client helpers (browser)

    Consider the implementations:

    // /lib/Helpers.js
    Helpers = {/* functions */}; // Assigned to window.Helpers and global.Helpers
    // /server/Helpers.js
    Helpers = _.extend(Helpers, {/*more functions*/}
    // /client/Helpers.js
    Helpers = _.extend(Helpers, {/*more functions*/}

    Trivial example:

    A load order not _.extend() in /lib/Helpers.js

    // /lib/Helpers.js
    // Helpers = {/* functions */}; // Overwrites...
    Helpers = _.extend(Helpers, {/* functions */}); // ReferenceErro

    Get a ReferenceError from Node if Helpers not defined – specifically the “Helpers” used as an argument. Node knows to assign Helpers as global.Helpers.


    Answered on December 24, 2018.
    Add Comment

  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.