I am using requirejs + jquery + jqueryui. I read TONS examples of how to do this. I think I understand the different approaches, and it seems to me that my setup should work correctly. However, I sometimes get $. The widget does not detect an error in my custom modules depending on jquery-ui. This is pain because it is inconsistent and difficult to reproduce, so it is difficult for me to test alternative approaches.
I do not smooth out all my jquery plugins, because there are a lot of them. Instead, I load jquery with a separate call to requirejs. Then, in the callback, I download the rest of my stuff. This way, I don't need to maintain a list of pads for all my jquery plugins.
For jquery-ui, I use a padding so that it depends on jquery. Then all my custom modules that use the factory widget have jquery-ui in the dependency list.
In my templates ...
requirejs.config({ baseUrl: ATHLETE.siteConfig.jsBaseUrl, paths: { 'jquery': '//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min', 'jquery-ui': '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min' }, shim: { "jquery-ui": ['jquery'] }, waitSeconds: 15 }); requirejs(['jquery'], function($) { requirejs(['site'], function() { requirejs(['mypage']); }); });
Please note that I am loading site.js before mypage.js. They have common dependencies. In my build configuration, I exclude site.js from mypage.js, so common dependencies are compiled into site.js, not mypage.js. Therefore, I need to fully download site.js before downloading mypage.js, otherwise you may need to download these general dependencies separately.
Here is an example of a custom module that depends on jquery-ui.
define([ 'jquery', 'jquery-ui' ],function($) { $.widget('ui.viewAllSponsorsWidget', $.ui.dialog, { options: { autoOpen: false, dialogClass: 'view-all-sponsor-dialog-wrap', draggable: false, modal: true, resizable: false, width: 370, height: 400 } }); });
The $ .widget error is undefined, caused by the 5th line of this and similar custom modules. Again, this is really inconsistent and difficult to reproduce. More often than not, I DO NOT get an error even when I clear my cache. Can anyone think that line 5 can be executed before jquery-ui is fully loaded?
UPDATE August 16, 2013
I was able to track this a bit more. I created a simple module that depends on jquery and jquery-ui.
define([ 'jquery', 'jquery-ui' ],function($) { console.log('$.widget is defined? ' + Boolean($.widget)); console.log('jQuery.widget is defined? ' + Boolean(jQuery.widget)); });
The result of this is as follows:
LOG: $.widget is defined? false LOG: jQuery.widget is defined? true
So, somehow the global jQuery object has a widget, but the copy provided to me by requirejs does not.