Best way to pack javascript library that requires jQuery?

I am writing a very basic JavaScript library that uses the $ .ajax () function for jQuery.

How do I manage this dependency? Should I instruct users of my library to enable jQuery? Should I use something like RequireJS or a tag insert script to load jQuery in a library? If the latter is better, how can I do this without causing conflict if the user is already using jQuery?

+7
source share
3 answers

I think it depends on whether you have more dependencies besides jQuery.

If jQuery is your only dependency, and your library really doesn't need its own module dependency system, I would not recommend RequireJS. Just check for jQuery in your library and otherwise make a mistake.

If you, however, want to create a flexible and supported library, I would recommend using some module loader (e.g. RequireJS). It also gives you the advantage of using a build system that lets you consolidate and package your library.

+2
source

In the end, I wrote my own function to extract JSON data as silly little ones recommended in the original post. Thanks to all who responded. The JavaScript library dependency guide was very valuable, although I took this other route.

I used this answer as a guide to writing my own function to extract JSON. I needed to synchronize the data, so I adjusted the function with the tips mentioned in this other article .

In the end, my function looked like this. Hope this helps someone else who comes.

var fetchJSON = function(path, callback) { var httpRequest = new XMLHttpRequest(); httpRequest.open('GET', path, false); httpRequest.send(); if (httpRequest.readyState === 4) { if (httpRequest.status === 200) { var data = JSON.parse(httpRequest.responseText); if (callback) callback(data); } } } 
+1
source

I would advise you to first give advice to users to enable jquery. If you let me choose any example, you will see that this is a really used approach (e.g. .net framework)

0
source

All Articles