Skip to content

Instantly share code, notes, and snippets.

@robhurring
Created July 7, 2015 13:59
Show Gist options
  • Save robhurring/87f2b4e0f2806ab407e5 to your computer and use it in GitHub Desktop.
Save robhurring/87f2b4e0f2806ab407e5 to your computer and use it in GitHub Desktop.
AJAX content replace
(function($) {
// scan our ajax response for replaceable content blocks then update the HTML
var updateContent = function(content) {
var $contentKeyElements = $(content).filter('[data-content-key]');
$contentKeyElements.each(function() {
var node = $(this);
var key = node.attr('data-content-key');
$('[data-content-key=' + key + ']').replaceWith(node);
});
$(document).trigger('content-updated');
};
$(function() {
$(document).bind('ajax:success', function(e, content) {
$.each(content, function(key, value) {
updateContent(value);
});
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment