Skip to content

Instantly share code, notes, and snippets.

@jtangelder
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtangelder/fa94f44bff8386052da5 to your computer and use it in GitHub Desktop.
Save jtangelder/fa94f44bff8386052da5 to your computer and use it in GitHub Desktop.
django.jQuery 1.4.2 upgrade functions to support some 'newer' jQuery methods.
(function(jQuery) {
if(!jQuery) {
return;
}
var $version = jQuery.prototype.jquery;
/**
* find out if a version number is greater then or equal (>=) as the compare version
* @example
* gteVersion('1.4.2', '1.4.3') => false
* gteVersion('1.4.2', '1.4.2') => true
* gteVersion('1.4.2', '1.4.10') => false
* @param check
* @param compare
* @returns {boolean}
*/
function gteVersion(check, compare) {
check = check.split(/\./g);
compare = compare.split(/\./g);
var i = 0;
while(i<Math.max(check.length, compare.length)) {
if((+check[i]||0) < (+compare[i]||0)) {
return false;
}
i++
}
return true;
}
/**
* add support for on and off methods
* @type {Function|*|on}
*/
jQuery.prototype.on = jQuery.prototype.on || function(/* events [,selector] [,data], handler */) {
var args = arguments;
// delegation bind has minimal 3 arguments
if(args.length >= 3) {
var events = args[0],
selector = args[1],
data = (args[3]) ? args[2] : null,
handler = (args[3]) ? args[3] : args[2];
this.bind(events, data, function(ev) {
var $target = jQuery(ev.target).closest(selector);
if($target.length) {
handler.call($target[0], ev);
}
});
} else {
this.bind.apply(this, args);
}
return this;
};
jQuery.prototype.off = jQuery.prototype.off || function(/* events [,selector] [,handler] */) {
if(arguments.length == 3) {
throw new Error("Delegated .off is not implemented.");
} else {
this.unbind.apply(this, arguments);
}
return this;
};
/**
* extend the existing .data method, by looking up html5 data attributes
* only for 1.4.2 and lower
*/
if(!gteVersion('1.4.3', $version)) {
jQuery.prototype.data = (function (originalDataMethod) {
// the parse value function is copied from the jQuery source
var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/;
function parseValue(data) {
if (typeof data === "string") {
try {
data = data === "true" ? true :
data === "false" ? false :
data === "null" ? null :
// Only convert to a number if it doesn't change the string
+data + "" === data ? +data :
rbrace.test(data) ? jQuery.parseJSON(data) : data;
} catch (e) {}
} else {
data = undefined;
}
return data;
}
return function (name, val) {
var result = originalDataMethod.apply(this, arguments);
// only when it's an getter and the result from the original data method is null
if (result === null && val === undefined) {
var attrValue = this.attr("data-" + name);
return parseValue(attrValue);
}
return result;
}
})(jQuery.prototype.data);
}
})(window.django && django.jQuery || window.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment