Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example remotable.js for Protytpe 1.6 and jQuery 1.3.2 with livequery
jQuery.ajaxSetup({
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript");}
});
$('a[data-remote="true"]').live('click', function(e) {
var $link = $(this);
var method = $link.attr('data-method') || 'get';
var url = $link.attr('href');
var type = (method == 'get') ? 'GET' : 'POST';
var token = $link.attr('data-authenticity_token');
var data = "_method=" + method;
data += '&authenticity_token=' + token;
$.ajax({
type: type,
url: url,
data: data,
dataType: 'script',
beforeSend: function() {
$link.trigger('link:beforeSend');
},
success: function() {
$link.trigger('link:success');
},
error: function(response, status, error) {
$link.trigger('link:error', [response, status, error]);
},
complete: function() {
$link.trigger('link:complete');
}
});
e.preventDefault();
});
$("form[data-remote='true']").livequery('submit',function(e){
$this = $(this);
var method = $this.find('input[name=_method]').val() ||
$this.attr('data-method') ||
$this.attr('method');
var url = $this.attr('action');
var type = (method == 'get') ? 'GET' : 'POST';
var data = $this.serialize() + "&_method=" + method;
$.ajax({
type: type,
url: url,
data: data,
dataType: 'script',
beforeSend: function() {
$this.trigger('form:beforeSend');
},
success: function() {
$this.trigger('form:success');
},
error: function(response, status, error) {
$this.trigger('form:error', [response, status, error]);
},
complete: function() {
$this.trigger('form:complete');
}
});
e.preventDefault();
});
});
document.observe("dom:loaded", function () {
$(document.body).observe("click", function(event) {
var $link = event.findElement("a");
if($link && 'true' == $link.readAttribute('data-remote')) {
var method = $link.readAttribute('data-method') || "get";
var token = $link.readAttribute('data-authenticity_token') || null;
var params = {authenticity_token: token};
if (method == 'get') {
params = null;
}
new Ajax.Request($link.readAttribute('href'), {
method: method,
parameters: params,
onLoaded: function() {
$link.fire('link:beforeSend');
},
onSuccess: function() {
$link.fire('link:success');
},
onFailure: function(response, status, error) {
$link.fire('link:error', [response, status, error]);
},
onComplete: function() {
$link.fire('link:complete');
}
});
event.stop();
}
elem = event.findElement('input');
var form = elem && elem.up('form[data-remote="true"]');
if(elem && 'submit' == elem.readAttribute('type') && form) {
form.request(
{onLoaded: function() {
$link.fire('form:beforeSend');
},
onSuccess: function() {
$link.fire('form:success');
},
onFailure: function(response, status, error) {
$link.fire('form:error', [response, status, error]);
},
onComplete: function() {
$link.fire('form:complete');
}
});
event.stop();
}
});
$(document.body).observe("keydown", function(event) {
if(event.keyCode == Event.KEY_RETURN) {
var elem = event.findElement('input');
if(elem) {
elem.up('form').request();
event.stop();
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.