1
$.getScript(app.modulePath+'/'+ moduleName+'.js')   //include file
    .done(function()
{
    app.loadedModules.push(moduleName);             //track loaded modules
    app.currentModule = moduleName;                 //assign module name to object property
});

I am loading javascript modules as I need them, but am having trouble referencing them. To load the module by name, I use a string and assign the string as the currentModule. But it's a string. How to I assign the loaded object which has the same name as the string.

Example:

Images.js

var Images = (function()
{

 return {
    allImages: function(){},
    oneImage: function(){}
 };

});

app.js

var moduleName = 'Images';
$.getScript(moduleName).done(function()
{
   myApp.currentModule = moduleName;
   //how to reference currentModule as object
});
4
  • 1
    i think what you are looking for is require.js Commented Jul 4, 2015 at 16:19
  • You can use http://www.w3schools.com/jsref/jsref_eval.asp Commented Jul 4, 2015 at 16:20
  • I'll be honest, I get what require.js is for, which is my situation here but its really hard to use. I'll consider it after I get a little further. Thanks. Commented Jul 4, 2015 at 16:33
  • 1
    @Sacreyoule Suggesting eval with a link from w3schools. That's some truly high level advice you've given. Commented Jul 4, 2015 at 17:05

2 Answers 2

2

You can use the global object:

window[moduleName].allImages();
Sign up to request clarification or add additional context in comments.

Comments

1

From what I understand your problem is creating the module object and assgin it right ? If im mistaken let me know in comments

So here is the soltion to your problem

app.js

var moduleName = 'Images';
$.getScript(moduleName+'.js').done(function()
{
   myApp.currentModule = new window[moduleName]();
   console.log(myApp.currentModule)
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.