Let's say I want to use jquery along with a standard jquery plugin that included non-amd, which was defined using standard closure: (function($))( $.fn.myplugin = { ... } )(jQuery); and all this is inside js/libs/jquery/jquery.myplugin.js .
I am using this configuration:
require.config({ baseUrl: 'js/', paths: { 'jquery': 'libs/jquery/jquery-noconflict', 'underscore': 'libs/underscore/underscore', 'backbone': 'libs/backbone/backbone', 'jquery-myplugin': 'libs/jquery/jquery.myplugin' }, shim: { 'backbone': { deps: ['underscore', 'jquery'], exports: 'Backbone' }, 'jquery-myplugin': { deps: ['jquery'] } });
I load jQuery in conflict free mode in libs/jquery/jquery-noconflict.js because I don't want to pollute the global namespace:
define(['libs/jquery'], function () { return jQuery.noConflict(true); });
and this is how I download the main app.js application:
define([ 'jquery', 'underscore', 'backbone', 'jquery-myplugin'], function($, _, Backbone, MyPlugin){ //MyPlugin is always undefined, not even sure if //I should be passing it here if it only extends jQuery? });
Now here is the problem I am experiencing - although I can use all the libraries defined above without any problems, I could not develop the correct shim configuration to load jquery plugins that do not support AMD.
I tried to configure jquery-myplugin as deps jquery (and vice versa), but I could never get it to work.
It seems that I have a problem with the following scenario:
- jQuery loads in non-conflict mode.
- plugin code is run extending the jQuery instance above
- I can use $ in my application, extended by the plugin code, so $ .myplugin is available.
I have seen similar questions floating around, but none of them actually solve this problem, giving only vague suggestions like "use shim config" ...
Edit
I also tried using
"jquery-myplugin": { deps: ["jquery"], exports: "jQuery.fn.myplugin" }
And although plugin methods are available after loading as an AMD module in this way, I still cannot access: $('.class').myplugin() by default, the $ object was not extended by myplugin code.