ajax-manager
A jQuery ajax management tool
install
npm install ajax-manager
useage
import ajaxManager from 'ajax-manager'
ajaxManager(models, globalConfig, jQuery)
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 apisglobalConfig
: Object
, required, all the config set here will effect all the backend api callingjquery
: jQuery
, required, give me your jQuery!
import ajaxManager from 'ajax-manager'
import $ from 'jquery'
var userModels = {
getUserInfoById: {
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 = {
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 eventtriggerGlobalEvents
: Boolean
, indicates if the ajax trigger global events
This function return an jquery ajax object so it can call done()
, fail()
, always()
function chainly.
apis.getUserInfoById({
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. All the config will be merged in one object just like Object.assign(globalConfig, modelConfig, apiCallingConfig)
except events and data
.
Events
There are 3 kinds of events
- Global events
beforeSend
success
error
complete
- Start and stop events
- 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)
Data
All the data
field in those 3 configs will merge into an object(Just like Object.assign(globalConfigData, modelConfigData, apiCallingConfigData)
) insteadof cover one by one.
var userModels = {
getUsers: {
methods: 'GET',
url: 'http://127.0.0.1/get_users',
data: {
origin: 'user'
}
}
}
var globalConfig = {
data: {
type: 'app'
}
}
var apis = ajaxManager([userModels], globalConfig, $)
api.getUsers({
data: {
auth: '123456abcdef'
}
}, true, true)
__apiRoot
Use __apiRoot
field to indicate the backend api root, it will concat in front of the url
. GlobalConfig, modelConfig and apiCallingConfig are all accept this field.
var userModels = {
getUsers: {
methods: 'GET',
url: '/get_users',
},
getUserInfoById: {
methods: 'GET',
url: '/get_user_info_by_id'
}
}
var globalConfig = {
__apiRoot: 'http://192.168.0.1'
}
var apis = ajaxManager([userModels], globalConfig, $)
api.getUsers({
data: {
auth: '123456abcdef'
}
}, true, true)
__ignoreGlobalApiRoot
Even if you have set an __apiRoot
in globalConfig, some of your backend apis may not need it. So you can set __ignoreGlobalApiRoot
to let an api ignore the global api root. This field is accepted in both GlobalConfig, modelConfig and apiCallingConfig.
var userModels = {
getUsers: {
methods: 'GET',
url: '/get_users',
},
getUserInfoById: {
methods: 'GET',
url: 'http://rx.top/get_user_info_by_id',
__ignoreGlobalApiRoot: true
}
}
var globalConfig = {
__apiRoot: 'http://192.168.0.1'
}
var apis = ajaxManager([userModels], globalConfig, $)
api.getUsers({
data: {
auth: '123456abcdef'
}
}, true, true)
TODO LIST: