Last active
September 29, 2015 12:38
-
-
Save gigi81/1601992 to your computer and use it in GitHub Desktop.
jQuery UI unobtrusive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
var createWidgets = function ($el) { | |
var data = $el.attr('data-jqueryui'); | |
var widget = data, options = null, index = data.indexOf(':'); | |
if (index > 0) { | |
widget = data.substr(0, index); | |
eval("options = " + data.substr(index + 1).trim()); | |
} | |
// Sanity check: can't directly check that it's truly a _widget_, but | |
// can at least verify that it's a defined function on jQuery: | |
if (typeof $.fn[widget] !== 'function') { | |
if (console) { | |
console.log("Could not find '" + widget + "' jQuery UI widget"); | |
} | |
return; | |
} | |
$el[widget](options); | |
}; | |
// override dialog standard behaviour | |
var dialog = $.fn.dialog; | |
$.fn.dialog = function() { | |
var ret = dialog.apply(this, arguments); | |
$(this).find('[data-jqueryui]').each(function() { | |
createWidgets($(this)); | |
}); | |
return ret; | |
}; | |
$.fn.unobtrusive = function () { | |
return $(this).find('[data-jqueryui]').each(function() { | |
createWidgets($(this)); | |
}); | |
}; | |
$(document).ready(function () { | |
$(document).unobtrusive(); | |
}); | |
} (jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment