Re: Smartmenus will not work when Jquery is loaded by Requirejs

Home Forums Older releases 0.9.x Smartmenus will not work when Jquery is loaded by Requirejs Re: Smartmenus will not work when Jquery is loaded by Requirejs

#1493
admin
Keymaster

OK, unfortunately, I don’t have access to your code base so I cannot give you a clear answer what exactly might be wrong with your configuration but here is a quick example how you could include jQuery and some plugin (SmartMenus in our case) using RequireJS (this covers just the JS files, you still need to include the CSS files, etc.)

Let’s assume you have the following files:

/index.html
/js/require.config.js
/js/main.js
/js/lib/jquery-1.10.2.js
/js/lib/jquery.smartmenus.min.js
/js/lib/require.js

in “index.html” you need just the following script reference:

<script data-main="js/require.config" src="js/lib/require.js"></script>

The content of /js/require.config.js:

requirejs.config({
    "baseUrl": "js/lib", // you may need to change this when running on a server to something like '/js/lib'
    "paths": {
        "jquery": "jquery-1.10.2",
        "jquery.smartmenus": "jquery.smartmenus.min"
    },
    shim: {
        // tell RequireJS that SmartMenus needs to be loaded after jQuery
        "jquery.smartmenus": {
            deps: [ "jquery" ],
            exports: "jQuery.fn.smartmenus"
        }    
    }
});

// Load main
requirejs(["../main"]);

The content of /js/main.js:

define(["jquery", "jquery.smartmenus"], function($) {
    // jquery.smartemnus.min.js has been loaded so init the menu
    $(function() {
        $('#main-menu').smartmenus({
            subMenusSubOffsetX: 1,
            subMenusSubOffsetY: -8
        });
    });
});

And the files in /js/lib/ are the plain jQuery, SmartMenus jQuery and RequireJS files.

Hope this helps. If you have any further issues, please provide me with some sort of access to your code base so that I could test and try to find out what could be wrong.

Cheers!

Register

You will be emailed a link to set your password.

Login

Lost password?