40

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

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

I have configured jQuery UI like this:

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 addClass not the animated one from jQuery UI.

PS: I use the full jQuery edition.

2
  • 5
    off-subject: white is a really bad classname; classnames should not include the css that is assigned to it so you can change the underlying CSS and not affect the semantic value of the class. Commented Aug 24, 2012 at 16:26
  • posible duplicate: stackoverflow.com/questions/10237384/… Commented Aug 24, 2012 at 16:29

3 Answers 3

34

You need to include jquery-ui:

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

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

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

Sign up to request clarification or add additional context in comments.

9 Comments

Shouldn't need to list underscore? Since it's already listed as a dependency of backbone?
IDK if jquery-ui will correctly add itself to $ if jQuery doesn't get assigned to $ in the closure
No, actually they do not need to be assigned at all. They assign themselves to the window object (window.$, window._, window.Backbone). Require works by passing in the return value of the included file, these libraries return nothing, but set their global references.
What if different versions are needed, compatibility is on, or multiple $ libraries are used?
Regardless, this is not how require works with these libraries. If this was kept as a function, the values passed in would be null because the scripts return nothing, but preform a global action.
|
3

try

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

2 Comments

Don't need jquery, all the other ones already list it as a dependency.
I've never used requirejs before, so I am just shooting in the dark, but I was concerned about $ being defined for jquery-ui to add itself to $.ui within the closure.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.