Skip to content

Commit

Permalink
added ajax deferred
Browse files Browse the repository at this point in the history
  • Loading branch information
james2doyle committed Mar 19, 2014
1 parent 0b7aa7a commit aca44e9
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 13 deletions.
44 changes: 42 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ $.when(late(1000), late(1200)).then(function(resolvedValue1, resolvedValue2) {

The beast. Easy ajax functions.

**Note:** these are *not* deferred. The reason? the deferred.js script makes up about half of the compiled `ki.extend.js` file.
**Note:** by default, deferred Ajax is included in the build. If you **are not using Deferred** then just uncomment the normal ajax lib and comment off Deferred and Ajax-Deferred.

Simple `POST`. Data is automagically `$.param'd` and sent with a request header of `('Content-type', 'application/x-www-form-urlencoded')`.

Expand Down Expand Up @@ -119,6 +119,45 @@ $.ajax('http:https://example.com/save', { id: 456 }, function(res, err) {
});
```

---

Deferred Examples

```javascript
$.ajax('form.php', { id: 123 }).done(function(res){
console.log(res);
});
```

Simple `GET`. If you do not pass an object, than the request is treated as a `GET` request.

```javascript
$.ajax('json.js').done(function(res) {
console.log(res);
});
```

Simple error handling. The second argument in the callback is an error callback.

```javascript
$.ajax('http:https://example.com/save', { id: 456 }).then(function(res) {
alert('I am ok!');
},
function(res) {
alert('Got an error, bro.');
});
```

Using `$.when`:

```javascript
$.when($.ajax('text.txt'), $.ajax('json.js')).then(function(res1, res2){
console.log(res1, res2);
}, function(res){
console.log(res);
});
```

### Building

You can choose to build a custom version of the extend lib. Just open the `gruntfile` and remove items from the concat task.
Expand All @@ -142,7 +181,8 @@ Here is the default list:
'build/parts/is.js',
'build/parts/map.js',
'build/parts/stop.js',
'build/parts/ajax.js',
// 'build/parts/ajax-deferred.js',
// 'build/parts/ajax.js',
'build/ki-deferred-js/deferred.js',
'build/parts/footer.js'
```
Expand Down
48 changes: 48 additions & 0 deletions build/parts/ajax-deferred.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
$.param = function(obj, prefix) {
var str = [];
for(var p in obj) {
var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
str.push(typeof v == "object" ? $.param(v, k) : encodeURIComponent(k) + "=" + encodeURIComponent(v));
}
return str.join("&");
};

// var p = new $.Deferred();
// p.resolve(n);
// return p.promise();

$.ajax = function(a, b, c) {
var xhr = new XMLHttpRequest();
var p = new $.Deferred();
// 1 == post, 0 == get
var type = (typeof(b) === 'object') ? 1: 0;
var gp = ['GET', 'POST'];
xhr.open(gp[type], a, true);
var cb = (!type) ? b: c;
if (typeof(c) === 'undefined' && typeof(b) !== 'function') {
cb = function(){};
}
xhr.onerror = function() {
p.reject(this);
cb(this, true);
};
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 400){
p.resolve(this.response);
cb(this.response, true);
} else {
p.reject(this);
cb(this, true);
}
}
};
if (type) {
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send($.param(b));
} else {
xhr.send();
}
xhr = null;
return p.promise();
};
3 changes: 2 additions & 1 deletion gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ module.exports = function(grunt) {
'build/parts/is.js',
'build/parts/map.js',
'build/parts/stop.js',
'build/parts/ajax.js',
// 'build/parts/ajax.js',
'build/parts/ajax-deferred.js',
'build/ki-deferred-js/deferred.js',
'build/parts/footer.js'
],
Expand Down
17 changes: 14 additions & 3 deletions ki.extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,22 +183,32 @@ $.param = function(obj, prefix) {
return str.join("&");
};

$.ajax = function(a, b, c, d) {
// var p = new $.Deferred();
// p.resolve(n);
// return p.promise();

$.ajax = function(a, b, c) {
var xhr = new XMLHttpRequest();
var p = new $.Deferred();
// 1 == post, 0 == get
var type = (typeof(b) === 'object') ? 1: 0;
var gp = ['GET', 'POST'];
xhr.open(gp[type], a, true);
xhr.responseType = (typeof(c) === 'string') ? c: '';
var cb = (!type) ? b: c;
if (typeof(c) === 'undefined' && typeof(b) !== 'function') {
cb = function(){};
}
xhr.onerror = function() {
p.reject(this);
cb(this, true);
};
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 400){
cb(this.response, false);
p.resolve(this.response);
cb(this.response, true);
} else {
p.reject(this);
cb(this, true);
}
}
Expand All @@ -210,6 +220,7 @@ $.ajax = function(a, b, c, d) {
xhr.send();
}
xhr = null;
return p.promise();
};

/**
Expand Down
2 changes: 1 addition & 1 deletion ki.extend.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit aca44e9

Please sign in to comment.