简体   繁体   中英

How do you manage dependencies in Javascript libraries

I am making a jQuery widget that will distributed to third parties. It is pretty simple, it will make an ajax call and put some content on a div on their page.

Is there an easy way a can give them everything they need in one file without the possibility of namespace conflicts?

I know I could make one file that has minimized jQuery, jQuery UI, and my own code, but that might create conflicts if they have other versions of jQuery. (For example, I might base my code off jQuery 1.7, and they might be running jQuery 1.3 for some reason.) So are there any good solutions to my problem?

I know its been a long time since you posted this question, but just in case anyone get here from google as I did, this is the solution you are looking for:

http://www.angrycoding.com/2011/09/managing-dependencies-with-requirejs.html

Summing up, you can use require to load a different jquery version for your app only, so it doesn't conflict with legacy code.

You should not create a minified version that has external libraries in it. Instead, clearly state your dependencies in your README. If someone uses an insufficient version of jQuery, tough luck.

For a more general approach at managing dependencies, have a look at require.js

Maybe using

var $j = jQuery.noConflict();

// Use jQuery via $j(...)
$j(document).ready(function(){
    $j("div").hide();
});

From http://api.jquery.com/jQuery.noConflict/

You can use jquery.noconflict to de-reference the last included version of jquery.

Directly after you include your version of jquery do something similar to:

var yournamespace = jQuery.noConflict();

then instead of the $ jquery operator you use your namespace similar to:

yournamespace("div p").hide();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM