RequireJS在其他文件中使用path

时间:2013-06-23 16:24:23

标签: jquery requirejs

我有一个用cakephp,backbone和requirejs开发的网站。

进入我的配置文件我已经映射了我的javascript,如果我将jquery调用到这个文件中,一切正常。 如果我将jquery调用到另一个文件中,请给我一个错误,该函数是未定义的,就像没有加载jquery一样。

这是我的fole工作正常config.js:

require.config({
    baseUrl: "/site.com/js/",

    paths: {
        // Aliases for libraries, so that we can change versions from here
        jquery: "libs/jquery/jquery-2.0.2.min",
        handlebars: "libs/backbone/template/handlebars",
        lodash: "libs/backbone/template/lodash.min",
        backbone: "libs/backbone/backbone-1.0.0.min",
        less:"libs/less/less-1.4.0-beta.min",
        helper:"app/helper",
        jquery_cookie:"libs/jquery/plugin/jquery.cookie",
        text:"libs/require/text-2.0.7"
    },

    shim: {
        "lodash": { exports: '_' },
        "handlebars": { exports: 'Handlebars' },
        "jquery": { exports: '$' },
        "backbone": {
            //These script dependencies should be loaded before loading
            //backbone.js
            deps: ["helper", "lodash", "jquery"],
            //Once loaded, use the global "Backbone" as the
            //module value.
            exports: "Backbone"
        },
        "jquery_cookie": {
            deps: ["jquery"]
            //,exports:"$"
        },
        "less": {
            exports:"less"
        }
    }

    // This is appended to every module loading request, for cache invalidation purposes
    // comment it on production
    , urlArgs: "bust=" + (new Date()).getTime()
});

require(["jquery"], function ($) {
    $("body").empty();
});

相反,如果我把它写入config.js

require.config({
    baseUrl: "/site.com/js/",

    paths: {
        // Aliases for libraries, so that we can change versions from here
        jquery: "libs/jquery/jquery-2.0.2.min",
        handlebars: "libs/backbone/template/handlebars",
        lodash: "libs/backbone/template/lodash.min",
        backbone: "libs/backbone/backbone-1.0.0.min",
        less:"libs/less/less-1.4.0-beta.min",
        helper:"app/helper",
        jquery_cookie:"libs/jquery/plugin/jquery.cookie",
        text:"libs/require/text-2.0.7"
    },

    shim: {
        "lodash": { exports: '_' },
        "handlebars": { exports: 'Handlebars' },
        "jquery": { exports: '$' },
        "backbone": {
            //These script dependencies should be loaded before loading
            //backbone.js
            deps: ["helper", "lodash", "jquery"],
            //Once loaded, use the global "Backbone" as the
            //module value.
            exports: "Backbone"
        },
        "jquery_cookie": {
            deps: ["jquery"]
            //,exports:"$"
        },
        "less": {
            exports:"less"
        }
    }

    // This is appended to every module loading request, for cache invalidation purposes
    // comment it on production
    , urlArgs: "bust=" + (new Date()).getTime()
});

将调用jquery转移到default.ctp中,检索函数undefined的错误,因为我没有加载jQuery。

default.thtml中

require(["jquery"], function ($) {
    $("body").empty();
});

为什么我不能将jquery用于deafult.ctp? 我要映射到所有文件?我不这么认为,有人可以帮助我吗? 感谢

1 个答案:

答案 0 :(得分:0)

在您尝试使用jquery之后,似乎您的config.js运行。 运行配置文件时,请尝试检查日志。