Skip to content

Commit

Permalink
axboot.ajax, axboot.promise 패치
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasJang committed Sep 6, 2017
1 parent 7965543 commit 36611b2
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 126 deletions.
144 changes: 81 additions & 63 deletions ax-boot-admin/src/main/webapp/assets/js/axboot/src/modules/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @param {String} http.url
* @param {Object|String} http.data
* @param {Function} http.callback
* @param {Function} http.onError
* @param {Function} http.always
* @param {Object} [http.options]
* @param {Boolean} [http.options.nomask = false]
* @param {Function} [http.options.onError]
Expand Down Expand Up @@ -38,84 +40,100 @@
* });
* ```
*/

axboot.ajax = (function () {

var queue = [];
var defaultOption = {
apiType: "",
contentType: 'application/json'
};
var queue = [];
var defaultOption = {
apiType: "",
contentType: 'application/json; charset=UTF-8'
};

return function (http) {
var jqxhr, httpOpts, callback;
var options = $.extend(true, {}, defaultOption, http.options);
if (!options.nomask) axAJAXMask.open();
return function (http) {
var jqxhr, httpOpts, callback, onerror, always;
var options = $.extend(true, {}, defaultOption, http.options);
if (!options.nomask) axAJAXMask.open();

queue.push("1");
httpOpts = {
contentType: options.contentType
};

httpOpts = {
contentType: options.contentType
};
var url = "";

var url = "";
if (ax5.util.isArray(http.url)) {
if (http.url[0] in axboot.def["API"]) {
http.url[0] = axboot.def["API"][http.url[0]];
http.url = CONTEXT_PATH + http.url.join('/');
} else {
http.url = CONTEXT_PATH + http.url.join('/');
}
} else {
http.url = CONTEXT_PATH + http.url;
if (ax5.util.isArray(http.url)) {
if (http.url[0] in axboot.def["API"]) {
http.url[0] = axboot.def["API"][http.url[0]];
http.url = CONTEXT_PATH + http.url.join('/');
} else {
http.url = CONTEXT_PATH + http.url.join('/');
}
} else {
http.url = CONTEXT_PATH + http.url;
}

if (http.single) {
var keepGoing = true;
queue.forEach(function (n) {
if (n.k === http.type + "~" + http.url) {
keepGoing = false;
}
});
if (!keepGoing) return false;
}

$.extend(http, httpOpts);
queue.push({k: http.type + "~" + http.url});

callback = http.callback;
$.extend(http, httpOpts);

jqxhr = $.ajax(http);
jqxhr.done(function (data, textStatus, jqXHR) {
if (typeof data == "string") {
arguments[0] = data = (data == "") ? {} : JSON.parse(data);
}
callback = http.callback;
always = http.always;
onerror = http.onError || options.onError;

if (data.redirect && options.apiType != "login") {
location.href = data.redirect;
return;
}
jqxhr = $.ajax(http);
jqxhr
.done(function (data, textStatus, jqXHR) {
if (typeof data == "string") {
arguments[0] = data = (data == "") ? {} : JSON.parse(data);
}

if (data.error) {
if (options.onError) {
options.onError(data.error);
} else {
alert(data.error.message);
if (data.error.requiredKey) {
$('[data-ax-path="' + data.error.requiredKey + '"]').focus();
}
}
} else {
var args = [].concat($.makeArray(arguments));
if (callback) callback.apply(this, args); // callback
if (data.redirect && options.apiType != "login") {
location.href = data.redirect;
return;
}

if (data.error) {
if (onerror) {
onerror(data.error);
} else {
alert(data.error.message);
if (data.error.requiredKey) {
$('[data-ax-path="' + data.error.requiredKey + '"]').focus();
}
}
} else {
var args = [].concat($.makeArray(arguments));
if (callback) callback.apply(this, args); // callback
}

}).fail(function (data, textStatus, msg) {
if (msg == "") {
})
.fail(function (data, textStatus, msg) {
if (msg == "") {

}
else {
if (callback) callback.apply(this, [{
error: {message: msg}
}]); // callback
}
}).always(function (data, textStatus, jqXHR) {
queue.pop();
}
else {
if (onerror) onerror({message: msg}); // callback
}
})
.always(function (data, textStatus, jqXHR) {
queue.pop();

// 프레임 셋에 타이머 초기화.
if (top.fnObj && top.fnObj.activityTimerView) {
top.fnObj.activityTimerView.update();
}
// 프레임 셋에 타이머 초기화.
if (top.fnObj && top.fnObj.activityTimerView) {
top.fnObj.activityTimerView.update();
}

if (!options.nomask) if (queue.length == 0) axAJAXMask.close(300);
});
}
if (always) always.apply(this, [data, textStatus, jqXHR]); // always
if (!options.nomask) if (queue.length == 0) axAJAXMask.close(300);
});
}
})();
111 changes: 48 additions & 63 deletions ax-boot-admin/src/main/webapp/assets/js/axboot/src/modules/promise.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
axboot.promise = function () {

/**
* @Class axboot.promise
* @example
* ```js
* axboot.promise()
* .then(function (ok, fail, data) {
/**
* @Class axboot.promise
* @example
* ```js
* axboot.promise()
* .then(function (ok, fail, data) {
* $.ajax({
* url: "/api/v1/connections",
* callback: function (res) {
Expand All @@ -16,7 +15,7 @@ axboot.promise = function () {
* }
* });
* })
* .then(function (ok, fail, data) {
* .then(function (ok, fail, data) {
* $.ajax({
* url: "/api/v1/login",
* data: data,
Expand All @@ -28,68 +27,54 @@ axboot.promise = function () {
* }
* });
* })
* .then(function (ok, fail, data) {
* .then(function (ok, fail, data) {
* console.log("success");
* })
* .catch(function (res) {
* .catch(function (res) {
* alert(res.message);
* });
* ```
*/
const myClass = function () {
this.busy = false;
this.queue = [];

/**
* @method axboot.promise.then
* @param fn
* @returns {myClass}
*/
this.then = function (fn) {
this.queue.push(fn);
this.exec();
return this;
};
/**
* @method axboot.promise.exec
* @param data
*/
this.exec = function (data) {
if (this.busy) return this;
var Q = this.queue.shift(),
self = this;
* ```
*/
const myClass = function () {
this.busy = false;
this.queue = [];
this.then = function (fn) {
this.queue.push(fn);
this.exec({});
return this;
};
this.exec = function (data) {
if (this.busy) return this;
var Q = this.queue.shift(),
self = this;

if (Q) {
this.busy = true;
if (Q) {
this.busy = true;

try {
Q(
function (a) {
self.busy = false;
self.exec(a);
},
function (e) {
self._catch(e);
},
data
);
}
catch (e) {
this._catch(e);
}
} else {
this.busy = false;
}
};
/**
* @method axboot.promise.catch
* @param fn
*/
this.catch = function (fn) {
this._catch = fn;
};
try {
Q(
function (a) {
self.busy = false;
self.exec(a);
},
function (e) {
self._catch(e);
},
data || {}
);
}
catch (e) {
this._catch(e);
}
} else {
this.busy = false;
}
};
this.catch = function (fn) {
this._catch = fn;
};
};

return new myClass();
return new myClass();

};

0 comments on commit 36611b2

Please sign in to comment.