ajax-manager
Advanced tools
Comparing version 1.0.5 to 1.0.6
{ | ||
"name": "ajax-manager", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "An jQuery ajax management tool", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -180,3 +180,34 @@ # ajax-manager | ||
## `__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. | ||
```javascript | ||
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) | ||
// finally when fire ajax, the url will be 'http://192.168.0.1/get_users' and 'http://rx.top/get_user_info_by_id' | ||
``` | ||
# TODO LIST: | ||
* useage examples |
@@ -49,6 +49,7 @@ // use global count instead of jquery global event because it does't support cross domain request | ||
if (!window.__apiRoot__) window.__apiRoot__ = null | ||
function getApi (modelName, modelConfig = {}, registerConfig = {}, config = {}) { | ||
function getUrl (modelName, modelConfig = {}, registerConfig = {}, config = {}) { | ||
let windowApiConfig = window.__apiConfig__[modelName] | ||
if (!windowApiConfig) windowApiConfig = {} | ||
let api = windowApiConfig.url || | ||
let url = windowApiConfig.url || | ||
config.url || | ||
@@ -61,7 +62,7 @@ modelConfig.url || | ||
registerConfig.__apiRoot || '' | ||
let abandonGlobalApiRoot = windowApiConfig.abandonGlobalApiRoot || | ||
config.__abandonGlobalApiRoot || | ||
modelConfig.__abandonGlobalApiRoot || | ||
registerConfig.__abandonGlobalApiRoot | ||
return abandonGlobalApiRoot ? api : (apiRoot + api) | ||
let ignoreGlobalApiRoot = windowApiConfig.ignoreGlobalApiRoot || | ||
config.__ignoreGlobalApiRoot || | ||
modelConfig.__ignoreGlobalApiRoot || | ||
registerConfig.__ignoreGlobalApiRoot | ||
return ignoreGlobalApiRoot ? url : (apiRoot + url) | ||
} | ||
@@ -82,10 +83,10 @@ | ||
// globle config | ||
$.ajaxSetup(Object.assign({}, models.__globlal || {}, { | ||
// reset data and event | ||
data: null, | ||
beforeSend: null, | ||
success: null, | ||
error: null, | ||
complete: null | ||
})) | ||
// $.ajaxSetup(Object.assign({}, models.__globlal || {}, { | ||
// // reset data and event | ||
// data: null, | ||
// beforeSend: null, | ||
// success: null, | ||
// error: null, | ||
// complete: null | ||
// })) | ||
let globalEvents = { // events set in __global and register should be add in this array | ||
@@ -114,6 +115,6 @@ beforeSend: [], | ||
} | ||
let api = {} | ||
let apis = {} | ||
for (let key in models) { | ||
if (!/^__/g.test(key)) { | ||
api[key] = function (config = {}, triggerAjaxStartAndStopEvent = true, triggerGlobalEvents = true) { | ||
apis[key] = function (config = {}, triggerAjaxStartAndStopEvent = true, triggerGlobalEvents = true) { | ||
// merge event | ||
@@ -152,3 +153,3 @@ Object.keys(globalEvents).map(eventName => { | ||
{ | ||
url: getApi(key, models[key], registerConfig, config), | ||
url: getUrl(key, models[key], registerConfig, config), | ||
data: allData, | ||
@@ -193,5 +194,5 @@ beforeSend (xhr) { | ||
} | ||
return api | ||
return apis | ||
} | ||
export default apiRegister |
14354
212