Javascript 我如何在 requirejs 中使用 jquery ui

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12113172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:08:00  来源:igfitidea点击:

How do I use jquery ui with requirejs

javascriptjquery-uirequirejs

提问by bodokaiser

I want to use jQuery UI's addClassfunction in my application.

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

Beside I am using the normal jQuery, underscore, backbone all tiered together with requirejs.

除了我使用普通的 jQuery,下划线,主干都与 requirejs 一起分层。

I have configured jQuery UI like this:

我已经像这样配置了 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']
    }

});

In the application I do:

在应用程序中,我这样做:

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

Unfortunately this only does the normal addClassnot the animated one from jQuery UI.

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

PS: I use the full jQuery edition.

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

采纳答案by Austin

You need to include jquery-ui:

您需要包含 jquery-ui:

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

jquery should be required automatically as it is a dependency of jquery-ui

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

Additionally, none of these scripts return anything, but their variables are assigned to the window object. No need to assign them.

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

回答by nickaknudson

try

尝试

define(['jquery', 'jquery-ui', 'underscore', 'backbone'], function($, ui, _, Backbone) {
    // $.ui should be defined, but do
    // $.ui = ui if its not
    $('div').addClass('white');
});

回答by hendrixski

Sometimes you only need a small subsection of jQuery UI. For example, I recently needed sortable, but if I tried to load the whole thing then I got a conflict between $.button()on jquery-ui and $.button()in bootstrap. jQuery UI now comes with AMD support, So I used RequireJS' build tool r.js to build exactly the subset that I needed.

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