#12882: jQuery.noConflict() in admin brokes site specific code with jQuery
-------------------------------------------+--------------------------------
Reporter: [email protected] | Owner: jezdez
Status: assigned | Milestone: 1.2
Component: django.contrib.admin | Version: SVN
Resolution: | Keywords: jQuery admin
Stage: Accepted | Has_patch: 0
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
-------------------------------------------+--------------------------------
Changes (by robhudson):
* cc: [email protected] (added)
Comment:
It is possible to completely encapsulate Django's jQuery version and its
plugins, but I'm curious if that's the best thing to do here. If it's
known that the Django admin has jQuery available via `jQuery` in the
global namespace, custom admin site changes can code to that. I, for one,
like knowing I can use Django's version of jQuery for some custom widgets
and such.
However, if we do want to encapsulate things so different versions of
jQuery can co-exist, it is possible following this skeleton code:
{{{
(function(window, document, version, callback) {
var j, d;
var loaded = false;
if (!(j = window.jQuery) || version > j.fn.jquery || callback(j))
{
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "/path/to/jquery.js";
script.onload = script.onreadystatechange = function() {
if (!loaded && (!(d = this.readyState) || d ==
"loaded" || d == "complete")) {
callback((j =
window.jQuery).noConflict(1), loaded = true);
j(script).remove();
}
};
document.documentElement.childNodes[0].appendChild(script);
}
})(window, document, "1.4", function($, jquery_loaded) {
// plugin code goes here.
});
}}}
What this code does is checks if there is an existing version of jQuery
loaded and checks it meets a given minimum version and if so, uses that
jQuery. If it doesn't exist, or doesn't meet the minimum version, it
dynamically adds a script tag to load its own jQuery and uses that. The
version loaded is encapsulated within the anonymous function and will not
conflict with any other Javascript libraries (e.g. other versions of
jQuery, prototype, etc.).
It seems a bit overkill to me, but if it's the best option, feel free to
use it.
-Rob
--
Ticket URL: <http://code.djangoproject.com/ticket/12882#comment:10>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.