javascript - RequireJS calling callbacks before dependencies loadedresolved - Stack Overflow

I'm having an issue with RequireJS where my main.js script has a reference to a dependency, which

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?

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?

Share Improve this question asked Apr 8, 2011 at 4:56 StoiveStoive 11.3k4 gold badges26 silver badges32 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744126640a4559644.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信