Trouble combining Require.js and Backbone.js/Under

2019-01-28 20:44发布

I've read previous threads where other members have had identical error messages as me, but their accepted solution doesn't seem to help.

Basically I get this in Chrome console when I try to load index.html:

Uncaught Error: Module name "underscore" has not been loaded yet for context: _. Use require([])

A couple of seconds later this shows up:

Uncaught Error: Load timeout for modules: underscore,backbone

Chrome network tools doesn't show any anomalies everything up until day_view.js is loaded fine (200 OK).

File structure

enter image description here

index.html

...
<script>
        var require = {
            deps: ["jquery/jquery-min", "underscore/underscore-min", "backbone/backbone-min"]
        };
</script>
<script data-main="scripts/main" src="scripts/require.js"></script>
...

main.js

require.config({
    baseUrl: 'scripts',

    paths:{
        jquery:'jquery/jquery-min',
        underscore:'underscore/underscore-min',
        backbone:'backbone/backbone-min'
    },

    shim: {
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: ["underscore/underscore-min", "jquery/jquery-min"],
            exports: "Backbone"
        }
    },

    waitSeconds: 200
});

require(['day_view'], function (day_view) {
    function start() {
        day_view.render();
    }

    return {
        start:start
    };
});

day_view.js

define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
    function render() {
        ...
    }
...

3条回答
Evening l夕情丶
2楼-- · 2019-01-28 21:14

PS Good news, Underscore 1.6.0 now supports requirejs define !!!

versions below this require shims, or requiring underscore.js then blindly hoping that the "_" global variable hasn;t been smashed (which to be fair is a fair bet)

simply load it in by

  requirejs.config({
    paths: {
        "underscore": "PATH/underscore-1.6.0.min",
    }
  });

No shimming required :)

查看更多
Viruses.
3楼-- · 2019-01-28 21:20

This finally worked.

require.config({
    paths:{
        jquery:'jquery/jquery-min',
        underscore:'underscore/underscore-min',
        backbone:'backbone/backbone-min'
    },
    shim: {
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: ["underscore", "jquery"],
            exports: "Backbone"
        },
        waitSeconds: 15
    }
});

require(['day_view'], function (day_view) {
    function start() {
        day_view.show();
    }
    console.log(day_view); // Empty object here?
    return {
        start:start
    };
});

and

define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) { ...
查看更多
狗以群分
4楼-- · 2019-01-28 21:22

You can use the shim above, or you can also use jrburke's** AMD compatible fork of Backbone/Underscore:

https://github.com/amdjs/backbone

https://github.com/amdjs/underscore

This allows you to simply do:

require.config({
    paths:{
        jquery:'jquery/jquery-min',
        underscore:'underscore/underscore-min',
        backbone:'backbone/backbone-min'
    }
});

define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) { ...

Frankly, I found the fork easier/cleaner/more robust than using a shim.

** FYI jrburke is the creator of requirejs.

查看更多
登录 后发表回答