This repository was archived by the owner on Dec 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathjquery.restfulizer.js
More file actions
77 lines (67 loc) · 2.77 KB
/
jquery.restfulizer.js
File metadata and controls
77 lines (67 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*!
* RestfulizerJs 0.1
* http://ifnot.github.io/RestfulizerJs/
*
* Use jQuery
* http://jquery.com/
*
* Inspired by froztbytes works :
* https://gist.github.com/froztbytes/5385905
*
* Copyright 2014 Anael Favre and other contributors
* Released under the MIT license
* https://raw.github.com/Ifnot/RestfulizerJs/master/LICENCE
*/
(function ($) {
$.fn.extend({
restfulizer: function (options) {
var defaults = $.extend({
parse: false,
target: null,
method: "POST"
}, options);
return $(this).each(function () {
var options = $.extend({}, defaults);
var self = $(this);
// Try to get data-param into options
if (typeof(self.attr('data-method')) != "undefined") {
options.method = self.attr('data-method').toUpperCase();
}
if (typeof(self.attr('href')) != "undefined") {
options.target = self.attr('href');
}
// Parse href parameters and create an input for each parameter
var inputs = "";
if (options.parse) {
var paramsIndex = options.target.indexOf("?");
var hasParams = (paramsIndex > -1);
if (hasParams) {
var params = options.target.substr(paramsIndex + 1).split('#')[0].split('&');
options.target = options.target.substr(0, paramsIndex);
for (var i = 0; i < params.length; i++) {
var pair = params[i].split('=');
inputs += " <input type='hidden' name='" + decodeURIComponent(pair[0]) + "' value='" + decodeURIComponent(pair[1]) + "'>\n";
}
}
}
if (options.method == 'GET' || options.method == 'POST') {
var form = "\n" +
"<form action='" + options.target + "' method='" + options.method + "' style='display:none'>\n" +
inputs +
"</form>\n";
}
else {
var form = "\n" +
"<form action='" + options.target + "' method='POST' style='display:none'>\n" +
" <input type='hidden' name='_method' value='" + options.method + "'>\n" +
inputs +
"</form>\n";
}
self.append(form)
.removeAttr('href')
.attr('style', 'cursor:pointer;')
.attr('onclick', '$(this).find("form").submit();');
});
}
});
})(jQuery);