Skip to content

Instantly share code, notes, and snippets.

@akost
Forked from alkaruno/widgets.js
Created February 7, 2012 09:55
Show Gist options
  • Save akost/1758857 to your computer and use it in GitHub Desktop.
Save akost/1758857 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