40

我想addClass在我的应用程序中使用 jQuery UI 的功能。

除此之外,我使用的是普通的 jQuery、下划线、主干,所有这些都与 requirejs 一起分层。

我已经像这样配置了 jQuery UI:

require.config({

    deps: ["main"],

    paths: {
        "text": "lib/text"
        , "jquery": "lib/jquery"
        , "jquery-ui": "lib/jquery-ui"
        , "underscore": "lib/underscore"
        , "backbone": "lib/backbone"
        , "bootstrap": "lib/bootstrap"
        , "templates": "../templates"
    },

    shim: {
        "jquery-ui": {
            exports: "$",
            deps: ['jquery']
        },
        "underscore": {
            exports: "_"
        },
        "backbone": {
            exports: "Backbone",
            deps: ["underscore", "jquery"]
        },
        "bootstrap": ['jquery']
    }

});

在我做的应用程序中:

define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
    $('div').addClass('white');
});

不幸的是,这只是正常addClass的,而不是来自 jQuery UI 的动画。

PS:我使用完整的 jQuery 版本。

4

3 回答 3

34

您需要包括 jquery-ui:

define(['jquery-ui', 'backbone'], function() {
    $('div').addClass('white');
});

jquery 应该是自动需要的,因为它是 jquery-ui 的依赖项

此外,这些脚本都不返回任何内容,但它们的变量被分配给窗口对象。无需分配它们。

于 2012-08-24T16:26:09.270 回答
3

尝试

define(['jquery', 'jquery-ui', 'underscore', 'backbone'], function($, ui, _, Backbone) {
    // $.ui should be defined, but do
    // $.ui = ui if its not
    $('div').addClass('white');
});
于 2012-08-24T16:26:34.707 回答
0

有时您只需要一小部分 jQuery UI。例如,我最近需要可排序的,但如果我试图加载整个东西,那么我$.button()在 jquery-ui 和$.button()bootstrap 之间会发生冲突。jQuery UI 现在支持 AMD,所以我使用 RequireJS 的构建工具 r.js 来构建我需要的子集。

于 2015-04-17T18:38:10.673 回答