Skip to content

Instantly share code, notes, and snippets.

@mauricioszabo
Created October 20, 2010 16:41
Show Gist options
  • Save mauricioszabo/636777 to your computer and use it in GitHub Desktop.
Save mauricioszabo/636777 to your computer and use it in GitHub Desktop.
app/views/javascript/index.html.erb
(function () {
var applyToElements = function(bodies, selector, f) {
for(var i=0; i < bodies.length; i++) {
var elements = bodies[i].select(selector)
for(var i2=0; i2 < elements.length; i2++) {
f(elements[i2])
}
}
}
document.on('ajax:complete', function(event) {
var bodies = document.getElementsByTagName('body')
var otherId = '[id=' + event.element().readAttribute('update') + ']'
applyToElements(bodies, '*[data-update=true]' + otherId, function(e) {
e.innerHTML = event.memo.responseText
})
applyToElements(bodies, '*[data-update=top]' + otherId, function(e) {
e.innerHTML = event.memo.responseText + e.innerHTML
})
applyToElements(bodies, '*[data-update=bottom]' + otherId, function(e) {
e.innerHTML = e.innerHTML + event.memo.responseText
})
})
})();
class JavascriptController < ApplicationController
def index
end
def post
render :text => "#{rand(100000000)}<br />"
end
end
<h1>Javascript#index</h1>
<div data-update='bottom'>
Nada
</div>
<br />
<div data-update='true'>
Zero
</div>
<%= link_to 'Randomizar', post_path, :remote => true, 'data-update-success' => 'wow' %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment