Created
July 7, 2015 13:59
-
-
Save robhurring/87f2b4e0f2806ab407e5 to your computer and use it in GitHub Desktop.
AJAX content replace
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($) { | |
// 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