Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requirejs: jQuery is undefined

Somehow I always receive this error:

Uncaught ReferenceError: jQuery is not defined

I have the impression that jQuery (loaded via CDN) takes more time to load (confirmed by the network tab on Chrome). I run this locally on my PC, so that's why the CDN call will always be longer then the libraries. But isn't requirejs supposed to wait after jQuery is loaded before loading the other libraries?

My boot.js :

(function(){
    requirejs.config({
        baseUrl: '/assets/js/',
        paths: {
            'lib': 'lib/',
            'src': 'src/',
            'jquery': [
                '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min',
                'lib/jquery-1.9.1.min'
            ],
            'mootools': 'lib/mootools-core-1.4.5',
            'class.mutators': 'lib/Class.Mutators.jQuery'
            //'order': 'assets/js/lib/order',
        },
        shim: {
            'class.mutators': {
                deps: [
                    'mootools'
                ],
                exports: 'classmutators'
            },
            'underscore': {
                    exports: '_'
            }
        },
        waitSeconds: 15
    });

    requirejs([
        'jquery',
        'src/app-require'
    ], function($){
        $(document).ready(function(){
            var App = new $.App($('body'));
        });
    });

})();

My app-require.js :

define([
    'mootools',
    'class.mutators',
    'src/Tracker',
    'lib/jquery.easing.1.3',
    'lib/nivo/jquery.nivo.slider.pack',
    'lib/isotope/jquery.isotope.min',
    'lib/waypoints.min'
], function() {

var className = 'App';
//--
return $[className] = new Class({
    jQuery: className,

    Implements: [Options, Events],

    options: {},

    //-- init
    //---------------------------------------------
    initialize: function(el, options) {
              ...
    },

            ...
    });
});

Any ideas?

like image 245
Alex Beauchemin Avatar asked Oct 03 '22 21:10

Alex Beauchemin


1 Answers

jQuery is not in any dependencies. You should add it in the shim dependencies of mutator:

  shim: {
            'class.mutators': {
                deps: [
                    'jquery',
                    'mootools'
                ],
                exports: 'classmutators'
            },
            'underscore': {
                    exports: '_'
            }
        },

Here is the official example of how to handle a jQuery dependency with shim: https://github.com/requirejs/example-jquery-shim#how-its-set-up

like image 143
Antoine Avatar answered Oct 26 '22 20:10

Antoine