13

I want to include jQueryUI in my backbone.js app using RequireJS. The main.js file included in my index.html is as follows:

require.config({
    paths: {
            jquery: 'libs/jquery/jquery-1.7.2.min',
            jqueryui: 'libs/jquery/jquery-ui-1.8.18.custom.min',
            underscore: 'libs/underscore/underscore-min',
            backbone: 'libs/backbone/backbone-optamd3-min',
            text: 'libs/require/text',
            templates: 'templates'
       }

});

require(['app'], function(App){
    App.start();
});

And for each model/view/router file, I just include the 'jquery' namespace at the start of the "define" block as follows:

define([
    'jquery',
    'underscore',
    'backbone',
    'views/categoryview',
    'text!templates/category.html'
    ], function($, _, Backbone, CategoryView, categoryTemplate){
        // Here comes my code
});

But the jQueryUI could not be used in these files. Is there something wrong with my code? Or should I also include the "jqueryui" in each "define" block? But if I include "jqueryui" in the "define" block, How should I name it in the function to avoid name conflict with jquery?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
chaonextdoor
  • 5,019
  • 15
  • 44
  • 61

4 Answers4

4

While kujakettu's answer is at least partially correct I also had to specify jQuery as a dependancy in my shim to be sure jQuery was loaded before jQuery-UI.

e.g.

require.config({
    baseUrl: 'scripts/modules',
    paths:{
        jquery:'../libs/jquery',
        jqueryUI:"../libs/jquery-ui",
        underscore:'../libs/underscore',
        backbone:'../libs/backbone'
    },
    shim: {
        jqueryUI: {
            deps: ['jquery']
        },
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: ["underscore", "jquery"],
            exports: "Backbone"
        },
        waitSeconds: 15
    }
});
MdaG
  • 2,680
  • 2
  • 34
  • 46
3

Quite old post. There's a tool available to convert jquery ui files to AMD version, created by jrburke. Hope it would be useful!

Aakash Goel
  • 1,169
  • 2
  • 13
  • 22
2

You can include all files that only patch some other object (like jQuery UI) in your main file as follows (you need to make sure jQuery is loaded before jQuery UI):

require(['jquery', 'app', 'jqueryui'], function ($, App) { App.start(); });

Another approach is to include jQuery UI in every module as you already mentioned.

I personally prefer first approach even though it's hiding dependencies.

T J
  • 42,762
  • 13
  • 83
  • 138
kujakettu
  • 118
  • 2
  • So what you mean is to include jqueryui in my main.js file as you provided and in every module, there is no need to worry about the jqueryui, I just need to include the jquery and everything will be up and running? – chaonextdoor Apr 20 '12 at 19:23
  • Yes, when you include jquery ui the first time, it adds more stuff to the original jQuery-object. After that, all ui functionality will be available with the jQuery-object. Same applies if you have other jquery plugins. – kujakettu Apr 21 '12 at 09:14
  • 9
    This approach is **wrong** requirejs does not guarantee that jquery gets loaded before jqueryui. It would be much better to use a shim config with a manual dependency. – Willem D'Haeseleer Jun 03 '13 at 05:37
  • @WillemD'Haeseleer This answer clearly states *"you need to make sure jQuery is loaded before jQuery UI"* shim config is a way of doing that so technically there is nothing wrong with the answer. – T J Jul 04 '16 at 07:47
  • Well your answer is at least incomplete then. You should at least mention how you can make sure jqueryui is loaded before jquery. I also believe this form of decency hiding can lead to problems in larger projects. – Willem D'Haeseleer Jul 04 '16 at 15:47
  • 1
    @WillemD'Haeseleer That's correct, for older versions. From 1.11.0 onwards shim is no longer required so *now* (I know your comment was old) the answer is ok. BTW it's not my answer. Those using old versions can make use of http://stackoverflow.com/a/17275596/2333214 for the shim config – T J Jul 05 '16 at 10:51
0

The other answers are a bit outdated now. JQuery UI comes with AMD support. If you want the whole project just use bower install jquery-ui and then add it to paths. No shim needed.

If you want to load a subset of jQuery UI that you need in your app (while not loading extra bloat) then just use bower install jquery-ui to get the whole thing then use RequireJS build tool called r.js to create an AMD package of exactly the files you need.

T J
  • 42,762
  • 13
  • 83
  • 138
hendrixski
  • 1,124
  • 1
  • 12
  • 20