Joe
Joe

Reputation: 8042

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?

Upvotes: 2

Views: 1878

Answers (4)

Juanmi Rodriguez
Juanmi Rodriguez

Reputation: 597

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.

Upvotes: 2

RTulley
RTulley

Reputation: 175

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();

Upvotes: 1

MatuDuke
MatuDuke

Reputation: 5008

Maybe using

var $j = jQuery.noConflict();

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

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

Upvotes: 1

chiborg
chiborg

Reputation: 28154

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

Upvotes: 1

Related Questions