user1184100
user1184100

Reputation: 6894

Not able to call another module in requirejs

In below code I'm calling myscript.getNum(); but myscript.js throws an error saying

Unexpected Token (

jloader.js

require(["evtManager","myscript"] , function(evtManager , myscript) {

var el = document.getElementById("Clickarea");
evtManager.addEvent(el , "click" , function(){
    myscript.getNum();
    }); 
});

myscript.js ->loads myscript2.js and calls a function getValue()

define(["myscript2"], function(myscript2) 
{
    getNum : function(){            //Throws an error "Unexpected token (" 
        var x = require("myscript2").getValue();
        return 5 + x;
    }
});

myscript2.js

define({
    getValue : function(){
         return 30;
    }
})

Upvotes: 0

Views: 350

Answers (2)

CgodLEY
CgodLEY

Reputation: 994

You probably want myscript.js to return an object similar to this example. I don't believe you need to require "myscript2" as that has already been done for you:

define(["myscript2"], function(myscript2)
{
    return {
        getNum : function(){
            var x = myscript2.getValue();
            return 5 + x;
        }
    };
});

Upvotes: 1

Bruno Schäpper
Bruno Schäpper

Reputation: 1302

Your example, the important part:

function() { getNum : function(){ } }

How is that supposed to work? The colon is actually totally wrong there. You have to use an equal sign. You can either use var getNum = function() for a private method, or this.getNum = function() for a public method.

Upvotes: 0

Related Questions