ajax-manager
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "ajax-manager", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "An jQuery ajax management tool", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
109
README.md
@@ -13,8 +13,105 @@ # ajax-manager | ||
ajaxManager(models, config, jQuery) | ||
ajaxManager(models, globalConfig, jQuery) | ||
``` | ||
# TOTO LIST: | ||
* make models support both Array and Object type | ||
* add default success and error handler | ||
* useage examples | ||
* finish ducoments | ||
# API | ||
## `ajaxManager()` | ||
Use `ajaxManager()` to register all your backend apis. It recieve there arguments: | ||
* `models`: `Array`, required, each of the element is an object contains a bounch of backend apis | ||
* `globalConfig`: `Object`, required, all the config set here will effect all the backend api calling | ||
* `jquery`: `jQuery`, required, give me your jQuery! | ||
```javascript | ||
import ajaxManager from 'ajax-manager' | ||
import $ from 'jquery' | ||
var userModels = { | ||
getUserInfoById: { | ||
// you can set any config that allowed in $.ajax() here | ||
methods: 'GET', | ||
url: 'http://127.0.0.1/get_user_info_by_id' | ||
}, | ||
getUsers: { | ||
methods: 'GET', | ||
url: 'http://127.0.0.1/get_users' | ||
}, | ||
register: { | ||
methods: 'POST', | ||
contentType: 'application/json' | ||
url: 'http://127.0.0.1/register' | ||
} | ||
} | ||
var globalConfig = { | ||
// you can set any config that allowed in $.ajax() here | ||
beforeSend () { | ||
console.log('ajax fire!') | ||
} | ||
} | ||
var apis = ajaxManager([userModels], globalConfig, $) | ||
``` | ||
## `API()` | ||
When apis register is done, the key of each api will be registered as a function of the returning object, and you can exce it to trigger ajax. | ||
It recieved 3 arguments: | ||
* `config`: `Object`, required, ajax config, | ||
* `triggerAjaxStartAndStop`: `Boolean`, indicates if the ajax trigger start and stop event | ||
* `triggerGlobalEvents`: `Boolean`, indicates if the ajax trigger global events | ||
This function return an jquery ajax object so it can call `done()`, `fail()`, `always()` function chainly. | ||
```javascript | ||
apis.getUserInfoById({ | ||
// you can set any config that allowed in $.ajax() here | ||
data: { | ||
id: 1 | ||
}, | ||
success (data, textStatus, qXHR) { | ||
console.log(success) | ||
}, | ||
error (jqXHR, textStatus, errorThrown) { | ||
console.log(fail) | ||
} | ||
}, ) | ||
.done((data, textStatus, jqXHR) => {}) | ||
.fail((jqXHR, textStatus, errorThrown) => {}) | ||
.always((data|jqXHR, textStatus, jqXHR|errorThrown) => {}) | ||
``` | ||
## Config | ||
You can write config in both model difinition config, `ajaxManager(m, globalConfig, j)` and `API(config)`.You can find all the accepted config in [jQuery.ajax](http://api.jquery.com/jQuery.ajax/). All the config will be merged in one object just like `Object.assign(ajaxManagerConfig, modelDifinitionConfig, apiCallingConfig)` except events. | ||
## Events | ||
There are 3 kinds of events | ||
* Global events | ||
* `beforeSend` | ||
* `success` | ||
* `error` | ||
* `complete` | ||
* Start and stop events | ||
* `ajaxStart` | ||
* `ajaxStop` | ||
* Local events | ||
* `beforeSend` | ||
* `success` | ||
* `error` | ||
* `complete` | ||
Global events only sopported in `ajaxManager()` config, those events will triggered by every ajax firing unless it set `triggerGlobalEvents` to `false`. | ||
Start and stop events only sopported in `ajaxManager()` config, this tow events only triggered when all the ajax start and all stoped(Just like `$( document ).ajaxStart()` and `$( document ).ajaxStop`). | ||
Local events supported in model definition config and `API()` config. The same event set in both model definition config and `API()` config will only triggered one(the `API()` config local event) | ||
# TODO LIST: | ||
* useage examples |
@@ -114,3 +114,3 @@ // use global count instead of jquery global event because it does't support cross domain request | ||
if (!/^__/g.test(key)) { | ||
api[key] = function (config = {}, triggerAjaxStartAndStopEvent = true) { | ||
api[key] = function (config = {}, triggerAjaxStartAndStopEvent = true, triggerGlobalEvents = true) { | ||
// merge event | ||
@@ -156,11 +156,11 @@ Object.keys(globalEvents).map(eventName => { | ||
} | ||
fireGlobalEvents(globalEvents.beforeSend, [xhr]) | ||
if (triggerGlobalEvents) fireGlobalEvents(globalEvents.beforeSend, [xhr]) | ||
fireLocalEvent(localEvents.beforeSend, [xhr]) | ||
}, | ||
success (data, statusText, xhr) { | ||
fireGlobalEvents(globalEvents.success, [data, statusText, xhr]) | ||
if (triggerGlobalEvents) fireGlobalEvents(globalEvents.success, [data, statusText, xhr]) | ||
fireLocalEvent(localEvents.success, [data, statusText, xhr]) | ||
}, | ||
error (xhr, statusText, error) { | ||
fireGlobalEvents(globalEvents.error, [xhr, statusText, error]) | ||
if (triggerGlobalEvents) fireGlobalEvents(globalEvents.error, [xhr, statusText, error]) | ||
fireLocalEvent(localEvents.error, [xhr, statusText, error]) | ||
@@ -173,3 +173,3 @@ }, | ||
} | ||
fireGlobalEvents(globalEvents.complete, [xhr, statusText]) | ||
if (triggerGlobalEvents) fireGlobalEvents(globalEvents.complete, [xhr, statusText]) | ||
fireLocalEvent(localEvents.complete, [xhr, statusText]) | ||
@@ -176,0 +176,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12130
116