jquery - RequireJS 使用其他文件中的路径

标签 jquery requirejs

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

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

这是我的 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 中,以获取函数未定义的错误,因为我认为未加载 jQuery。

默认.ctp

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

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

最佳答案

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

关于jquery - RequireJS 使用其他文件中的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17262783/

相关文章:

jquery - 如何让 jquery-mobile 和依赖插件与 require.js 一起使用?

javascript - 在调用函数时传递附加参数

javascript - 将 javascript 函数添加到父级

javascript - 在 React Js 中过滤 Todo 列表

javascript - 在每个循环内创建对象实例

javascript - 用 jQuery 条件替换 CSS 属性

javascript - JQuery 自动完成未使用所选项目填充输入

javascript - 通过 Grunt 替换 require.js 脚本标签

javascript - 如何使这个模块与 requireJS 兼容

javascript - 如何判断4个并行的 "require"回调是否全部完成?