From 2b307fe6dd334cb4c4d3877939e46c89e47b71ae Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 8 Jul 2015 05:26:31 -0400 Subject: [PATCH] feature(rest) callback called on success only -> callback called always --- lib/client.js | 2 +- lib/client/config.js | 5 ++-- lib/client/dom.js | 61 ++++++++++++++++++++++------------------- lib/client/markdown.js | 2 +- lib/client/menu.js | 4 ++- lib/client/operation.js | 20 ++++++++------ lib/client/rest.js | 4 ++- lib/client/upload.js | 4 ++- 8 files changed, 58 insertions(+), 44 deletions(-) diff --git a/lib/client.js b/lib/client.js index b9fd8e91..78640479 100644 --- a/lib/client.js +++ b/lib/client.js @@ -371,7 +371,7 @@ var Util, DOM, CloudFunc, join; if (!isRefresh && json) createFileTable(obj, panel, options, callback); else - RESTful.read(path, 'json', function(obj) { + RESTful.read(path, 'json', function(error, obj) { createFileTable(obj, panel, options, function() { var current; diff --git a/lib/client/config.js b/lib/client/config.js index b73bf264..e53f7cf0 100644 --- a/lib/client/config.js +++ b/lib/client/config.js @@ -194,8 +194,9 @@ var CloudCmd, Util, DOM, io; function saveHttp(obj) { var RESTful = DOM.RESTful; - RESTful.Config.write(obj, function() { - onSave(obj); + RESTful.Config.write(obj, function(error) { + if (!error) + onSave(obj); }); } diff --git a/lib/client/dom.js b/lib/client/dom.js index c95efa55..31a8ae19 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -457,8 +457,8 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; path += type; if (name) - RESTful.write(path, function() { - CloudCmd.refresh(null, function() { + RESTful.write(path, function(error) { + !error && CloudCmd.refresh(null, function() { var current = DOM.getCurrentByName(name); DOM.setCurrentFile(current); @@ -514,8 +514,8 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; Images.show.load('top'); - op(fileFrom, function() { - CloudCmd.refresh(null, function() { + op(fileFrom, function(error) { + !error && CloudCmd.refresh(null, function() { var file = DOM.getCurrentByName(name); DOM.setCurrentFile(file); @@ -678,10 +678,12 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; Images.show.load(); if (name !== '..') - RESTful.read(link + query, function(size) { - DOM.setCurrentSize(size, current); - Util.exec(callback, current); - Images.hide(); + RESTful.read(link + query, function(error, size) { + if (!error) { + DOM.setCurrentSize(size, current); + Util.exec(callback, current); + Images.hide(); + } }); }; @@ -696,9 +698,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; query = '?hash', link = DOM.getCurrentPath(current); - RESTful.read(link + query, function(data) { - callback(null, data); - }); + RESTful.read(link + query, callback); }; /** @@ -766,18 +766,21 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; path = DOM.getCurrentPath(current), isDir = DOM.isCurrentIsDir(current), - func = function(data) { + func = function(error, data) { var length, ONE_MEGABYTE = 1024 * 1024 * 1024; - if (Util.type.object(data)) - data = Util.json.stringify(data); + if (!error) { + if (Util.type.object(data)) + data = Util.json.stringify(data); + + length = data.length; + + if (hash && length < ONE_MEGABYTE) + DOM.saveDataToStorage(path, data, hash); + } - length = data.length; - if (hash && length < ONE_MEGABYTE) - DOM.saveDataToStorage(path, data, hash); - - Util.exec(callback, null, data); + callback(error, data); }; if (Info.name === '..') @@ -806,8 +809,8 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; if (!query) query = ''; - DOM.RESTful.write(url + query, data, function() { - DOM.saveDataToStorage(url, data); + DOM.RESTful.write(url + query, data, function(error) { + !error && DOM.saveDataToStorage(url, data); }); }; @@ -1459,16 +1462,18 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; to : dirPath + to }; - RESTful.mv(files, function() { + RESTful.mv(files, function(error) { var Storage = DOM.Storage, path = CloudFunc.rmLastSlash(dirPath); - DOM.setCurrentName(to, current); - Cmd.updateCurrentInfo(); - Storage.remove(path); - - if (isExist) - CloudCmd.refresh(); + if (!error) { + DOM.setCurrentName(to, current); + Cmd.updateCurrentInfo(); + Storage.remove(path); + + if (isExist) + CloudCmd.refresh(); + } }); } } diff --git a/lib/client/markdown.js b/lib/client/markdown.js index 940c07eb..963d1cc9 100644 --- a/lib/client/markdown.js +++ b/lib/client/markdown.js @@ -32,7 +32,7 @@ var CloudCmd, Util, DOM; if (o.relative) name += relativeQuery; - Markdown.read(name, function(result) { + Markdown.read(name, function(error, result) { var div = DOM.load({ name : 'div', className : 'help', diff --git a/lib/client/menu.js b/lib/client/menu.js index f2181bbd..c279d776 100644 --- a/lib/client/menu.js +++ b/lib/client/menu.js @@ -265,7 +265,9 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO; CloudCmd.execFromModule('FilePicker', 'saveFile', function(name, data) { var path = DOM.getCurrentDirPath() + name; - DOM.RESTful.write(path, data, CloudCmd.refresh); + DOM.RESTful.write(path, data, function(error) { + !error && CloudCmd.refresh(); + }); }); } diff --git a/lib/client/operation.js b/lib/client/operation.js index e29e7b23..eda0ad0b 100644 --- a/lib/client/operation.js +++ b/lib/client/operation.js @@ -239,17 +239,19 @@ if (!n) names = [Info.name]; - deleteFn(path + query, names, function() { + deleteFn(path + query, names, function(error) { var Storage = DOM.Storage, dirPath = Info.dirPath, dir = rmLastSlash(dirPath); - if (n > 1) - DOM.deleteSelected(files); - else - DOM.deleteCurrent(current); - - Storage.removeMatch(dir); + if (!error) { + if (n > 1) + DOM.deleteSelected(files); + else + DOM.deleteCurrent(current); + + Storage.removeMatch(dir); + } }); } @@ -315,10 +317,10 @@ names : names }; - operation(files, function() { + operation(files, function(error) { var path = rmLastSlash(from); - DOM.Storage.remove(path, function() { + !error && DOM.Storage.remove(path, function() { var panel = Info.panel, panelPassive = Info.panelPassive, setCurrent = function() { diff --git a/lib/client/rest.js b/lib/client/rest.js index b31ce784..69f9d9c1 100644 --- a/lib/client/rest.js +++ b/lib/client/rest.js @@ -197,6 +197,8 @@ var Util, DOM, CloudFunc, CloudCmd; setTimeout(function() { DOM.Dialog.alert(text); }, 100); + + p.callback(Error(text)); }, success : function(data) { Images.hide(); @@ -204,7 +206,7 @@ var Util, DOM, CloudFunc, CloudCmd; if (!p.notLog) CloudCmd.log(data); - Util.exec(p.callback, data); + p.callback(null, data); } }); } diff --git a/lib/client/upload.js b/lib/client/upload.js index 7e73eaa5..8f2605d8 100644 --- a/lib/client/upload.js +++ b/lib/client/upload.js @@ -54,7 +54,9 @@ var CloudCmd, Util, DOM; Upload.hide(); - DOM.uploadFiles(files); + DOM.uploadFiles(files, function(error) { + error && CloudCmd.log(error); + }); }); }