Skip to content

Commit

Permalink
Now using PUT and DELETE methods by default
Browse files Browse the repository at this point in the history
POST-tunnelling can be enabled by changing $.restSetup.useMethodOverride
Making difference between public & private functions more explicit
Making sure we check for custom csrf params, not just authenticity_token
Other refactoring for style, clarity
Some of these changes were discussed in #3, Closes #5
  • Loading branch information
alassek committed May 28, 2011
1 parent c86037f commit 73f6d89
Showing 1 changed file with 33 additions and 45 deletions.
78 changes: 33 additions & 45 deletions jquery.rest.js
Expand Up @@ -22,20 +22,28 @@

(function($){

var _ajax = $.ajax, trim;

// support JS < 1.8.1
trim = String.prototype.trim || function () {
return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

// Will only use method override if $.restSetup.useMethodOverride is set to true
// Change the values of this global object if your method parameter is different.
$.restSetup = { methodParam: '_method' };
$.restSetup = {
methodParam: '_method',
useMethodOverride: false
};

// collect csrf param & token from meta tags if they haven't already been set
$(document).ready(function(){
$.restSetup.csrfParam = $.restSetup.csrfParam || $('meta[name=csrf-param]').attr('content');
$.restSetup.csrfToken = $.restSetup.csrfToken || $('meta[name=csrf-token]').attr('content');
});

// jQuery doesn't provide a better way of intercepting the ajax settings object
var _ajax = $.ajax, options, trim;

function collect_options (url, data, success, error) {
options = { dataType: 'json' };
var options = { dataType: 'json' };
if (arguments.length === 1 && typeof arguments[0] !== "string") {
options = $.extend(options, url);
if ("url" in options)
Expand Down Expand Up @@ -63,6 +71,7 @@
}
});
}
return options;
}

function fill_url (url, data) {
Expand Down Expand Up @@ -90,15 +99,8 @@
return headers;
}

// support JS < 1.8.1
trim = String.prototype.trim || function () {
return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

// public functions

function ajax (settings) {
settings.type = settings.type || "GET";
$.ajax = function (settings) {
var csrfParam = new RegExp("(" + $.restSetup.csrfParam + "=)", "i");

if (typeof settings.data !== "string")
if (settings.data != null) {
Expand All @@ -108,59 +110,45 @@
settings.data = settings.data || "";
if ($.restSetup.csrfParam && $.restSetup.csrfToken)
if (!/^(get)$/i.test(settings.type))
if (!/(authenticity_token=)/i.test(settings.data)) {
if (!csrfParam.test(settings.data)) {
settings.data += (settings.data ? "&" : "") + $.restSetup.csrfParam + '=' + $.restSetup.csrfToken;
}

if ($.restSetup.useMethodOverride)
if (!/^(get|post)$/i.test(settings.type)) {
settings.beforeSend = function (xhr) {
xhr.setRequestHeader('X-HTTP-Method-Override', settings.type);
};
settings.data += (settings.data ? "&" : "") + $.restSetup.methodParam + '=' + settings.type.toLowerCase();
settings.type = "POST";
}

return _ajax.call(this, settings);
}

function read () {
collect_options.apply(this, arguments);
$.extend(options, { type: 'GET' })
$.read = function () {
var options = collect_options.apply(this, arguments);
options.type = 'GET';
return $.ajax(options);
}

function create () {
collect_options.apply(this, arguments);
$.extend(options, { type: 'POST' });
$.create = function () {
var options = collect_options.apply(this, arguments);
options.type = 'POST';
return $.ajax(options);
}

function update () {
collect_options.apply(this, arguments);
$.extend(options, {
type: 'PUT',
beforeSend: function (xhr) {
xhr.setRequestHeader('X-HTTP-Method-Override', 'PUT');
}
});
$.update = function () {
var options = collect_options.apply(this, arguments);
options.type = 'PUT';
return $.ajax(options);
}

function destroy () {
collect_options.apply(this, arguments);
$.extend(options, {
type: 'DELETE',
beforeSend: function (xhr) {
xhr.setRequestHeader('X-HTTP-Method-Override', 'DELETE');
}
});
$.destroy = function () {
var options = collect_options.apply(this, arguments);
options.type = 'DELETE';
return $.ajax(options);
}

$.extend({
ajax: ajax,
read: read,
create: create,
update: update,
destroy: destroy
});

})(jQuery);

0 comments on commit 73f6d89

Please sign in to comment.