Created
February 23, 2010 00:39
-
-
Save miketierney/311714 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| This is a collection of JS to help get jQuery integrated into a Rails project, specifically intended to be used with the jQuery.form plugin. |
This file contains hidden or 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
| <%= javascript_tag("var AUTH_TOKEN = #{form_authenticity_token.inspect};").untaint if protect_against_forgery? %> |
This file contains hidden or 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
| // jQuery initializations | |
| // TODO: Convert all $j back to $ when fully moved over to jQuery. | |
| // TODO: Move all of this in to application.js when Prototype is no longer a factor. | |
| // jQuery(document).ajaxSend(function (event, request, settings) { | |
| // if (typeof(AUTH_TOKEN) == "undefined") return; | |
| // settings.data = settings.data || ""; | |
| // settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIcomponent(AUTH_TOKEN); | |
| // }); | |
| // | |
| // // add javascript request type | |
| // jQuery.ajaxSetup({ | |
| // 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}, | |
| // }); | |
| jQuery.ajaxSetup({ 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} }) | |
| function _ajax_request(url, data, callback, type, method) { | |
| if (jQuery.isFunction(data)) { | |
| callback = data; | |
| data = {}; | |
| } | |
| return jQuery.ajax({ | |
| type: method, | |
| url: url, | |
| data: data, | |
| success: callback, | |
| dataType: type | |
| }); | |
| } | |
| jQuery.extend({ | |
| put: function(url, data, callback, type) { | |
| return _ajax_request(url, data, callback, type, 'PUT'); | |
| }, | |
| delete_: function(url, data, callback, type) { | |
| return _ajax_request(url, data, callback, type, 'DELETE'); | |
| } | |
| }); | |
| /* | |
| Submit a form with Ajax | |
| Use the class ajaxForm in your form declaration | |
| <% form_for @comment,:html => {:class => "ajaxForm"} do |f| -%> | |
| */ | |
| jQuery.fn.submitWithAjax = function() { | |
| this.unbind('submit', false); | |
| this.submit(function() { | |
| $j.post(this.action, $j(this).serialize(), null, "script"); | |
| return false; | |
| }) | |
| return this; | |
| }; | |
| /* | |
| Retreive a page with get | |
| Use the class get in your link declaration | |
| <%= link_to 'My link', my_path(),:class => "get" %> | |
| */ | |
| jQuery.fn.getWithAjax = function() { | |
| this.unbind('click', false); | |
| this.click(function() { | |
| $j.get($j(this).attr("href"), $j(this).serialize(), null, "script"); | |
| return false; | |
| }) | |
| return this; | |
| }; | |
| /* | |
| Post data via html | |
| Use the class post in your link declaration | |
| <%= link_to 'My link', my_new_path(),:class => "post" %> | |
| */ | |
| jQuery.fn.postWithAjax = function() { | |
| this.unbind('click', false); | |
| this.click(function() { | |
| $j.post($j(this).attr("href"), $j(this).serialize(), null, "script"); | |
| return false; | |
| }) | |
| return this; | |
| }; | |
| /* | |
| Update/Put data via html | |
| Use the class put in your link declaration | |
| <%= link_to 'My link', my_update_path(data),:class => "put",:method => :put %> | |
| */ | |
| jQuery.fn.putWithAjax = function() { | |
| this.unbind('click', false); | |
| this.click(function() { | |
| $j.put($j(this).attr("href"), $j(this).serialize(), null, "script"); | |
| return false; | |
| }) | |
| return this; | |
| }; | |
| /* | |
| Delete data | |
| Use the class delete in your link declaration | |
| <%= link_to 'My link', my_destroy_path(data),:class => "delete",:method => :delete %> | |
| */ | |
| jQuery.fn.deleteWithAjax = function() { | |
| this.removeAttr('onclick'); | |
| this.unbind('click', false); | |
| this.click(function() { | |
| $j.delete_($j(this).attr("href"), $j(this).serialize(), null, "script"); | |
| return false; | |
| }) | |
| return this; | |
| }; | |
| /* | |
| Ajaxify all the links on the page. | |
| This function is called when the page is loaded. You'll probaly need to call it again when you write render new datas that need to be ajaxyfied.' | |
| */ | |
| function ajaxLinks(){ | |
| $j('.ajaxForm').submitWithAjax(); | |
| $j('a.get').getWithAjax(); | |
| $j('a.post').postWithAjax(); | |
| $j('a.put').putWithAjax(); | |
| $j('a.delete').deleteWithAjax(); | |
| } | |
| $j(document).ready(function() { | |
| // All non-GET requests will add the authenticity token | |
| $j(document).ajaxSend(function(event, request, settings) { | |
| if (typeof(window.AUTH_TOKEN) == "undefined") return; | |
| // IE6 fix for http://dev.jquery.com/ticket/3155 | |
| if (settings.type == 'GET' || settings.type == 'get') return; | |
| settings.data = settings.data || ""; | |
| settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(window.AUTH_TOKEN); | |
| }); | |
| ajaxLinks(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment