-
-
Save akost/1758857 to your computer and use it in GitHub Desktop.
Widgets Library
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
/** | |
* 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