Loading mustache using requirejs

后端 未结 4 2199
青春惊慌失措
青春惊慌失措 2021-02-19 18:58

I would like to load and use mustache by requirejs.

Maybe this question has already asked:
AMD Module Loading Error with Mustache using RequireJS

Anyway I am

4条回答
  •  渐次进展
    2021-02-19 19:31

    Not sure if RequireJS 2.1.0 was out at the time of posting this question (and the answers) but the preferred way of handling this now is using shim config element (more info on project's docs page).

    Your main.js would become:

    require.config({
        paths: {
            jquery: 'libs/jquery/jquery',
            underscore: 'libs/underscore/underscore-min',
            backbone: 'libs/backbone/backbone-optamd3-min',
            mustache: "libs/mustache/mustache"
        },
        shim: {
            'mustache': {
                exports: 'Mustache'
            }
        }
    });
    (...)
    

    That's effectively the same as wrapper suggested @AntoJs, but without the boilerplate code.

    ...but then, since Mustache supports AMD there's no need to wrap/shim in the first place!

提交回复
热议问题