3

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

在我的配置文件中,我已经映射了我的 javascript,如果我将 jquery 调用到这个文件中,一切正常。如果我将 jquery 调用到另一个文件中,则会出现错误,即函数未定义,就像未加载 jquery 一样。

这是我在 config.js 中运行良好的 fole:

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 中检索我未定义函数的错误,因为它没有加载我认为的 jQuery。

默认.ctp

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

为什么我不能在 deafult.ctp 中使用 jquery?我必须映射到所有文件?我不这么认为,有人可以帮助我吗?谢谢

4

1 回答 1

0

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

于 2014-02-15T06:47:40.540 回答