Skip to content

Commit ccd7314

Browse files
authored
Merge pull request #5 from nsonic001/feature/more-http-methods
Added support for more HTTP verbs
2 parents 44cbf01 + b240d0b commit ccd7314

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ __<span style="color: rgb(180,0,0);">Twix</span>__ is a lightweight JavaScript l
66

77
It may weigh-in at just over 1KB, but this puppy has teeth. __WOOF!__
88

9-
Modelled after the ubiquitious jQuery's AJAX methods (_$.ajax_, _$.get_, and _$.post_), you can use this in places were jQuery will not load, such as __inside a WebWorker__<sup>#</sup>. You little ripper!
9+
Modelled after the ubiquitious jQuery's AJAX methods (_$.ajax_, _$.get_, _$.post_, _$.patch, _$.put_, _$.delete_ and _$.options_), you can use this in places were jQuery will not load, such as __inside a WebWorker__<sup>#</sup>. You little ripper!
1010

1111
You may be like me and occasionally think that loading the entire jQuery library (about 80KB minified) just to make an AJAX call is just like filling a thimble using a fire hydrant. __<span style="color: rgb(180,0,0);">Twix</span>__ is written from the ground up to work in modern browsers (IE9+, Firefox, Chrome, Safari, etc. Anything that supports __XMLHttpRequest__, really) and has just enough magic sauce to get the job done. No more, no less.
1212

twix.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var Twix = (function () {
3939
client.timeout = options.timeout;
4040
client.ontimeout = function () {
4141
options.error('timeout', 'timeout', client);
42-
}
42+
};
4343
}
4444
client.open(options.type, options.url, options.async);
4545

@@ -51,7 +51,7 @@ var Twix = (function () {
5151

5252
client.send(options.data);
5353
client.onreadystatechange = function() {
54-
if (this.readyState == 4 && this.status == 200) {
54+
if (this.readyState == 4 && ((this.status >= 200 && this.status < 300) || this.status == 304)) {
5555
var data = this.responseText;
5656
var contentType = this.getResponseHeader('Content-Type');
5757
if (contentType && contentType.match(/json/)) {
@@ -64,7 +64,7 @@ var Twix = (function () {
6464
};
6565

6666
if (options.async == false) {
67-
if (client.readyState == 4 && client.status == 200) {
67+
if (client.readyState == 4 && ((client.status >= 200 && client.status < 300) || client.status == 304)) {
6868
options.success(client.responseText, client);
6969
} else if (client.readyState == 4) {
7070
options.error(client.status, client.statusText, client);
@@ -73,33 +73,48 @@ var Twix = (function () {
7373

7474
return client;
7575
};
76-
77-
Twix.get = function(url, data, callback) {
76+
77+
var _ajax = function(type, url, data, callback) {
7878
if (typeof data === "function") {
7979
callback = data;
8080
data = undefined;
8181
}
82-
8382
return Twix.ajax({
8483
url: url,
8584
data: data,
85+
type: type,
8686
success: callback
8787
});
8888
};
89-
89+
90+
Twix.get = function(url, data, callback) {
91+
return _ajax("GET", url, data, callback);
92+
};
93+
94+
Twix.head = function(url, data, callback) {
95+
return _ajax("HEAD", url, data, callback);
96+
};
97+
9098
Twix.post = function(url, data, callback) {
91-
if (typeof data === "function") {
92-
callback = data;
93-
data = undefined;
94-
}
95-
96-
return Twix.ajax({
97-
url: url,
98-
type: 'POST',
99-
data: data,
100-
success: callback
101-
});
99+
return _ajax("POST", url, data, callback);
100+
};
101+
102+
Twix.patch = function(url, data, callback) {
103+
return _ajax("PATCH", url, data, callback);
102104
};
105+
106+
Twix.put = function(url, data, callback) {
107+
return _ajax("PUT", url, data, callback);
108+
};
109+
110+
Twix.delete = function(url, data, callback) {
111+
return _ajax("DELETE", url, data, callback);
112+
};
113+
114+
Twix.options = function(url, data, callback) {
115+
return _ajax("OPTIONS", url, data, callback);
116+
};
117+
103118

104119
return Twix;
105120
})();

twix.min.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
* THE SOFTWARE.
2424
*/
25-
var Twix=function(){function t(){}return t.ajax=function(t){t=t||{url:""},t.type=t.type.toUpperCase()||"GET",t.headers=t.headers||{},t.timeout=parseInt(t.timeout)||0,t.success=t.success||function(){},t.error=t.error||function(){},t.async="undefined"==typeof t.async?!0:t.async;var e=new XMLHttpRequest;t.timeout>0&&(e.timeout=t.timeout,e.ontimeout=function(){t.error("timeout","timeout",e)}),e.open(t.type,t.url,t.async);for(var s in t.headers)t.headers.hasOwnProperty(s)&&e.setRequestHeader(s,t.headers[s]);return e.send(t.data),e.onreadystatechange=function(){if(4==this.readyState&&200==this.status){var e=this.responseText;this.getResponseHeader("Content-Type").match(/json/)&&(e=JSON.parse(this.responseText)),t.success(e,this.statusText,this)}else 4==this.readyState&&t.error(this.status,this.statusText,this)},0==t.async&&(4==e.readyState&&200==e.status?t.success(e.responseText,e):4==e.readyState&&t.error(e.status,e.statusText,e)),e},t.get=function(e,s,r){return"function"==typeof s&&(r=s,s=void 0),t.ajax({url:e,data:s,success:r})},t.post=function(e,s,r){return"function"==typeof s&&(r=s,s=void 0),t.ajax({url:e,type:"POST",data:s,success:r})},t}();__=Twix;
25+
var Twix=function(){function t(){}t.ajax=function(t){t=t||{url:""},t.type=t.type.toUpperCase()||"GET",t.headers=t.headers||{},t.timeout=parseInt(t.timeout)||0,t.success=t.success||function(){},t.error=t.error||function(){},t.async="undefined"==typeof t.async?!0:t.async;var e=new XMLHttpRequest;t.timeout>0&&(e.timeout=t.timeout,e.ontimeout=function(){t.error("timeout","timeout",e)}),e.open(t.type,t.url,t.async);for(var s in t.headers)t.headers.hasOwnProperty(s)&&e.setRequestHeader(s,t.headers[s]);return e.send(t.data),e.onreadystatechange=function(){if(4==this.readyState&&(this.status>=200&&this.status<300||304==this.status)){var e=this.responseText,s=this.getResponseHeader("Content-Type");s&&s.match(/json/)&&(e=JSON.parse(this.responseText)),t.success(e,this.statusText,this)}else 4==this.readyState&&t.error(this.status,this.statusText,this)},0==t.async&&(4==e.readyState&&(e.status>=200&&e.status<300||304==e.status)?t.success(e.responseText,e):4==e.readyState&&t.error(e.status,e.statusText,e)),e};var e=function(e,s,n,r){return"function"==typeof n&&(r=n,n=void 0),t.ajax({url:s,data:n,type:e,success:r})};return t.get=function(t,s,n){return e("GET",t,s,n)},t.head=function(t,s,n){return e("HEAD",t,s,n)},t.post=function(t,s,n){return e("POST",t,s,n)},t.patch=function(t,s,n){return e("PATCH",t,s,n)},t.put=function(t,s,n){return e("PUT",t,s,n)},t["delete"]=function(t,s,n){return e("DELETE",t,s,n)},t.options=function(t,s,n){return e("OPTIONS",t,s,n)},t}();__=Twix;

0 commit comments

Comments
 (0)