RequireJs:使用自动加载-DEPS与垫片 [英] RequireJs: Use autoloading-deps with shim

查看:1579
本文介绍了RequireJs:使用自动加载-DEPS与垫片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了一个RequireJs配置定义路径和垫片:

I have defined a RequireJs configuration which defines paths and shims:

require.config({
    // define application bootstrap
    deps: ["main"],

    // define library shortcuts
    paths: {
        app: "app"
        , jquery: "lib/jquery"
        , underscore: "lib/underscore"
        , backbone: "lib/backbone"
        , bootstrap: "lib/bootstrap"
    },

    // define library dependencies
    shim: {
        jquery: {
            exports: "$"
        },
        underscore: {
            exports: "_"
        },
        backbone: {
            deps: ["underscore", "jquery"],
            exports: "Backbone"
        },
        bootstrap: {
            deps: ['jquery'],
            exports: "bootstrap"
        },

        // main application
        app: {
            deps: ["backbone"],
            exports: "App"
        }
    }
});

正如你所看到的最后一个垫片的宣言应该使它能够访问骨干网(它DEPS)当我加载主应用程序( - 命名空间)。

As you see the last "shim" declaration should make it able to access backbone (and it deps) when I load the main App(-namespace).

在现实中这不起作用:

require(["app"], function($, _, Backbone, App){
    app.router = new Backbone.Router.extend({
        // routing and route actions
    });
});

是什么让我疑惑的是,在脊梁-boilderplate - 项目,骨干(及其DEPS)可供选择这种方式:
<一href=\"https://github.com/tbranyen/backbone-boilerplate/blob/master/app/main.js\">https://github.com/tbranyen/backbone-boilerplate/blob/master/app/main.js

本甚至没有在功能来定义这个。

The not even had to define this in the function.

那我做错了吗?

推荐答案

删除 $,_,骨干α参数从要求,其中您扩展路由器 - 功能。垫片出口全局值,所以没有必要提及他们在要求定义要求像你这样做常规的依赖关系。

Remove the $, _, Backbone-parameters from the require-function where you extend the Router. The shims export global values, so there is no need to reference to them in require or define calls like you do for regular dependencies.

将它们作为参数与全局变量混乱,最有可能导致其受到不确定的。

Passing them as parameters messes with the global variables and most likely results in them being undefined.

这篇关于RequireJs:使用自动加载-DEPS与垫片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆