Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequireJS calling callbacks before dependencies loaded/resolved

I'm having an issue with RequireJS where my main.js script has a reference to a dependency, which is loaded but not resolved when the callback in main.js requesting this dependency is run.

My directory structure is:

index.htm
scripts/
    require.js
    main.js
    feeds/
        feed.js

index.htm:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Blah</title>
        <script data-main="scripts/main" src="scripts/require.js"></script>
    </head>
    <body>
        <h1>Hello, World!</h1>
    </body>
</html>

main.js:

require(["feeds/feed"], function(feed) {
    console.log("A");
    require.ready(function() {
        console.log("B");
        console.log(feed.val);
    });
});

feed.js:

console.log("C");
require(function() {
    console.log("D");
    return {
        val: "E"
    }
})

And the console output, suggesting that the dependency files are being loaded, but not resolved correctly:

C
A
B
Uncaught TypeError: Cannot read property 'val' of null

I must be missing something really obvious here, but whatever documentation I read up on, the problem doesn't seem to be revealing itself. Any ideas?

like image 350
Stoive Avatar asked Dec 16 '22 15:12

Stoive


1 Answers

You're using require to define your modules, where you should be using define.

like image 92
Stoive Avatar answered Apr 24 '23 08:04

Stoive