0

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.

4
  • 1
    The index.js includes a correct define() call? Commented Apr 10, 2015 at 14:25
  • Well no, do I need to include a define call? The page I'm loading isn't a dependency of another. Nothing will depend on either of these pages. They're quite standalone. Commented Apr 10, 2015 at 14:32
  • If you want to refer to the object returned via new page() as in your example, you have to use define(). Commented Apr 10, 2015 at 14:38
  • Okay, I'll give it a go quick. Post an answer though, so I can vote it up :P Commented Apr 10, 2015 at 14:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

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.