Skip to content

Instantly share code, notes, and snippets.

@alkaruno
Created January 16, 2012 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alkaruno/1620606 to your computer and use it in GitHub Desktop.
Save alkaruno/1620606 to your computer and use it in GitHub Desktop.
Widgets Library
/**
* Widgets Library
* @version 0.1
* @author Alexey Karunos (alkaruno@gmail.com)
* @example
* var widgets = new Widgets('http://site.com/widgets/');
* widgets.show('userinfo');
*/
;
function Widgets(urlPrefix, urlSuffix) {
this.show = function (id) {
var widgetId = 'widget-' + id;
var widgetUrl = (urlPrefix || '') + id + (urlSuffix || '');
document.write('<div id="' + widgetId + '" class="widget"></div>');
$('#' + widgetId).load(widgetUrl, function () {
$(this).find('form').data('url', widgetUrl);
});
};
$(function () {
$('.widget form').live('submit', function (e) {
e.preventDefault();
var $form = $(this).closest('form');
var action = $form.attr('action') || $form.data('url');
$.post(action, $form.serializeArray(), function (html) {
$form.closest('.widget').html(html).find('form').data('url', action);
}, 'html')
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment