Zander Rootman
Zander Rootman

Reputation: 2208

Require JS callback argument undefined

First off, I'm pretty new to Require JS, and I haven't done my fair share of reading the docs. Kind of just shooting from the hip here.

But this is functionality that, should work. Well as far as I've read.

I have a hashed URL, say at this stage it's #index. And then I have the equivalent js page under /javascript/pages/index.js.

As you would figure, I'm trying to load these pages "dynamically". However, my callback functions page parameter is undefined.

require(['javascript/pages/' + page],
    function(page) {
        var constructedPage = new page();
    });

All Pages are "classes" function index(){}

In the meanwhile, I'll start reading up on the docs a bit more.

Upvotes: 0

Views: 751

Answers (1)

Sirko
Sirko

Reputation: 74086

If you want to use objects/variables/etc created in the index.js within the callback of require(), you have to use a define() call to specify that object.

index.js

define(function(){

  // create an object with constructor
  function myPage(){
  }

  // some more code adding to the prototype

  // return the actual object
  return myPage;

});

Then you can use that object like you did in your code.

Note: That define() call may have dependencies of its own. Omitted here for simplicity's sake.

Upvotes: 1

Related Questions