Skip to content

Commit

Permalink
Added shorthands for the most common calls (GET, POST, PUT, DELETE)
Browse files Browse the repository at this point in the history
- Also fixed an issue where the defaults would be overridden by any ajax-call.
  • Loading branch information
fizker committed Nov 12, 2013
1 parent 5ac982f commit c946d18
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -19,8 +19,8 @@
, "mocha-as-promised": "~1.1.0"
, "chai": "~1.4.2"
, "chai-as-promised": "~3.2.3"
, "fzkes": "~0.9.0"
, "q": "0.8.11"
, "fzkes": "~0.10.1"
, "q": "~0.9.7"
}
, "keywords":
[ "browser"
Expand Down
11 changes: 11 additions & 0 deletions readme.md
Expand Up @@ -95,6 +95,17 @@ The options dictionary supports the following keys:
It will override `Content-Type` with `application/x-www-form-urlencoded`.


For convenience, there are some shorthands for the basic CRUD methods:

- `fajax.get()`
- `fajax.post()`
- `fajax.put()`
- `fajax.delete()` or `fajax.del()`

They all act as the primary function (`fajax()`), except they also enforce the
`method` option.


Support for promises
--------------------

Expand Down
43 changes: 37 additions & 6 deletions src/ajax.js
Expand Up @@ -38,14 +38,36 @@ var fajax = (function() {
qs = null
}

ajax.get = function(/*...args*/) {
var args = Array.prototype.slice.call(arguments)
var options = getOptions(args)
options.method = 'GET'
return this(options)
}
ajax.post = function(/*...args*/) {
var args = Array.prototype.slice.call(arguments)
var options = getOptions(args)
options.method = 'POST'
return this(options)
}
ajax.put = function(/*...args*/) {
var args = Array.prototype.slice.call(arguments)
var options = getOptions(args)
options.method = 'PUT'
return this(options)
}
ajax.del = ajax['delete'] = function(/*...args*/) {
var args = Array.prototype.slice.call(arguments)
var options = getOptions(args)
options.method = 'DELETE'
return this(options)
}

return ajax

function ajax(/*...args*/) {
var args = Array.prototype.slice.call(arguments)
var request = new XMLHttpRequest()
, addEventListener = 'addEventListener'
, opts = defaults
, arg
function getOptions(args) {
var opts = {}
var arg
while(arg = args.shift()) {
if(typeof(arg) == 'string') {
opts.url = arg
Expand All @@ -55,6 +77,15 @@ var fajax = (function() {
opts = merge(opts, arg)
}
}
return merge(defaults, opts)
}

function ajax(/*...args*/) {
var args = Array.prototype.slice.call(arguments)
var request = new XMLHttpRequest()
var addEventListener = 'addEventListener'
var opts = getOptions(args)

normalizeHeaders(opts)
prepareBody(opts)
if(request[addEventListener]) {
Expand Down
48 changes: 47 additions & 1 deletion test/unit/ajax.js
@@ -1,6 +1,6 @@
describe('unit/ajax.js', function() {
var fajax
, Response = require('../helpers/Response')
var Response = require('../helpers/Response')
before(function() {
global.XMLHttpRequest = require('../helpers/XMLHttpRequest')
fajax = require('../../src/ajax')
Expand All @@ -9,6 +9,52 @@ describe('unit/ajax.js', function() {
delete global.XMLHttpRequest
})

describe('When calling `fajax.get()`', function() {
beforeEach(function() {
req = fajax.get('/abc')
})
it('should act as a shorthand for `fajax(<url>, { method: “GET” })`', function() {
expect(req.request.open)
.to.have.been.calledWith('GET')
})
})
describe('When calling `fajax.post()`', function() {
beforeEach(function() {
req = fajax.post('/abc')
})
it('should act as a shorthand for `fajax(<url>, { method: “POST” })`', function() {
expect(req.request.open)
.to.have.been.calledWith('POST')
})
})
describe('When calling `fajax.put()`', function() {
beforeEach(function() {
req = fajax.put('/abc')
})
it('should act as a shorthand for `fajax(<url>, { method: “PUT” })`', function() {
expect(req.request.open)
.to.have.been.calledWith('PUT')
})
})
describe('When calling `fajax.del()`', function() {
beforeEach(function() {
req = fajax.del('/abc')
})
it('should act as a shorthand for `fajax(<url>, { method: “DELETE” })`', function() {
expect(req.request.open)
.to.have.been.calledWith('DELETE')
})
})
describe('When calling `fajax.delete()`', function() {
beforeEach(function() {
req = fajax['delete']('/abc')
})
it('should act as a shorthand for `fajax(<url>, { method: “DELETE” })`', function() {
expect(req.request.open)
.to.have.been.calledWith('DELETE')
})
})

describe('When using form option', function() {
var req
describe('with a JS object', function() {
Expand Down

0 comments on commit c946d18

Please sign in to comment.