|
|
adds ajax to all forms on a page within a div with 'jaxy' class name.
Form class name is the update target.
window.addEvent('domready', function(){
var forms = $$('.jaxy form');
forms.each(function(element){
element.addEvent('submit', function(e) {
new Event(e).stop();
var url = element.getProperty('action')
var rest = element.getProperty('rel')
var target = element.getProperty('class')
var box = $(target)
if (rest == 'delete') {
new Ajax(url, {
method: 'delete',
update: box,
onRequest: function(){
box.addClass('jaxy');
},
onComplete: function() {
box.removeClass('jaxy');
}
}).request();
}
else {
this.send({
update: box,
onComplete: function() {
box.removeClass('jaxy');
}
})
}
})
})
});
Mootools library. Easy enough to convert to prototype, these functions are very similar.
custom button_to helper goes in application helper. This allows rel and class name html
attributes to be set in the form tag generated by the button_to helper -- rel and class
name tell the ajax function which http method to use (ie, 'delete') and what element to
update with the response.
def button_to(name, fmeth, fclass, options = {}, html_options = {} )
html_options = html_options.stringify_keys
convert_boolean_attributes!(html_options, %w( disabled ))
method_tag = ''
if (method = html_options.delete('method')) && %w{put delete}.include?(method.to_s)
method_tag = tag('input', :type => 'hidden', :name => '_method', :value => method.to_s)
end
form_method = method.to_s == 'get' ? 'get' : 'post'
if confirm = html_options.delete("confirm")
html_options["onclick"] = "return #{confirm_javascript_function(confirm)};"
end
url = options.is_a?(String) ? options : self.url_for(options)
name ||= url
html_options.merge!("type" => "submit", "value" => name)
""
end
usage:
<%= button_to 'Destroy', 'delete', 'myupdatetarget', specialty_path(specialty), :method => :delete %>
where 'delete' adds rel="delete" and 'myupdatetarget' adds class="myupdatetarget" to the form
tag. The Rails button_to defaults only allow you to add a class name to the form input field.
|