From 747ed97be82aa5ec9083f6c98a8646b3f6af7323 Mon Sep 17 00:00:00 2001 From: Georg Tavonius Date: Sat, 12 Sep 2015 18:56:20 +0200 Subject: [PATCH] .ajaxSetup predefines all parameters --- README.md | 12 ++++++++++++ src/reqwest.js | 13 ++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ca75f9e..e75dcf8 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,18 @@ var r = reqwest({ * `complete` A function called whether the request is a success or failure. Always called when complete. * `jsonpCallback` Specify the callback function name for a `JSONP` request. This value will be used instead of the random (but recommended) name automatically generated by reqwest. +## Predefined options + +Use `.ajaxSetup` to set up predefined values for all options, like headers. + +``` js +reqwest.ajaxSetup({ + headers: { + 'X-CSRF-Token': document.querySelector('[name="csrf-token"]').content + } +}); +``` + ## Security If you are *still* requiring support for IE6/IE7, consider including [JSON3](https://bestiejs.github.io/json3/) in your project. Or simply do the following diff --git a/src/reqwest.js b/src/reqwest.js index 23d50df..3561d2f 100644 --- a/src/reqwest.js +++ b/src/reqwest.js @@ -253,8 +253,19 @@ } function init(o, fn) { + o = typeof o === 'string' ? { url: o } : o + for (var k in globalSetupOptions) { + if (typeof globalSetupOptions[k] === 'object') { + o[k] = o[k] ? o[k] : {} + for (var k2 in globalSetupOptions[k]) { + o[k][k2] = typeof o[k][k2] === 'undefined' ? globalSetupOptions[k][k2] : o[k][k2] + } + } else { + o[k] = typeof o[k] === 'undefined' ? globalSetupOptions[k] : o[k] + } + } - this.url = typeof o == 'string' ? o : o['url'] + this.url = o['url'] this.timeout = null // whether request has been fulfilled for purpose