Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

Commit

Permalink
Upgrade to jQuery 1.4.
Browse files Browse the repository at this point in the history
Use submit live events.
Remote events (click/submit) now signal which element was acted on using options.element;
  • Loading branch information
assaf committed Jan 16, 2010
1 parent 24188f0 commit 93e0b39
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
1 change: 0 additions & 1 deletion lib/action_view/helpers/unobtrusive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def form_remote_tag(options = {}, &block)
end
html["data-confirm"] = options.delete(:confirm)
html["data-remote"] = "true"
html["onsubmit"] = "$(this).trigger('remote');return false"
html.merge!(options)

form_tag(html.delete(:action) || url_for(url), html, &block)
Expand Down
16 changes: 6 additions & 10 deletions public/highfive.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
(function($) {
// Chrome likes to send the request as XML, which fails when there's no body
// (e.g. links that post). Not necessary with jQuery 1.4.
var setContentType = function(xhr) { xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") }

// Spooky action at a distance (link_to_remote).
$("a[data-remote]").live("remote", function() {
var link = $(this),
method = (link.attr("data-method") || "get").toLowerCase(),
question = link.attr("data-confirm"),
success = link.attr("data-success"),
error = link.attr("data-failure"),
options = { beforeSend: setContentType };
error = link.attr("data-failure");
options = { contentType: "application/x-www-form-urlencoded" };
if (!question || confirm(question)) {
options.url = link.attr("href");
if (method == "get" || method == "post")
Expand All @@ -19,7 +15,7 @@
options.type = "post"
options.data = { _method: method }
}
options.context = link;
options.element = link;
options.dataType = success ? "html" : "script";
options.success = success ?
function(response) { $(document.getElementById(success)).html(response) } :
Expand All @@ -40,7 +36,7 @@
success = form.attr("data-success"),
error = form.attr("data-failure"),
buttons = form.find(":submit:enabled,button:enabled"),
options = { beforeSend: setContentType };
options = { contentType: "application/x-www-form-urlencoded" };
if (!question || confirm(question)) {
options.url = form.attr("action");
options.data = form.serialize();
Expand All @@ -50,7 +46,7 @@
options.type = "post"
options.data = options.data + "&_method=" + method
}
options.context = form;
options.element = form;
options.dataType = success ? "html" : "script";
options.success = success ?
function(response) { $(document.getElementById(success)).html(response) } :
Expand All @@ -63,5 +59,5 @@
$.ajax(options);
}
return false
})
}).live("submit", function() { $(this).trigger("remote"); return false });
}(jQuery))

0 comments on commit 93e0b39

Please sign in to comment.