From edc8b36d4270fac73a1471800250458f8cd7f3c3 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 4 Nov 2013 14:57:01 +0000 Subject: [PATCH] feature(rest) add config --- lib/client/config.js | 15 ++++++++++++++- lib/client/dom.js | 9 +++++++++ lib/server/rest.js | 21 ++++++++++++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/client/config.js b/lib/client/config.js index 87394826..bd9fffd3 100644 --- a/lib/client/config.js +++ b/lib/client/config.js @@ -109,7 +109,20 @@ var CloudCmd, Util, DOM; } function change(event) { - Util.log(event); + var data, + obj = {}, + el = event.target, + name = el.id, + type = el.type; + + if (el.type === 'checkbox') + data = el.checked; + else + data = el.value; + + obj[name] = data; + + DOM.RESTful.config(obj); } function key(event) { diff --git a/lib/client/dom.js b/lib/client/dom.js index a2bc85a2..f8993021 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -181,6 +181,15 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; callback : pCallBack }); }; + + this.config = function(pData, pCallBack) { + sendRequest({ + method : 'PUT', + url : '/config', + data : pData, + callback : pCallBack + }); + }; function sendRequest(pParams) { var lRet = Util.checkObjTrue(pParams, ['method']); diff --git a/lib/server/rest.js b/lib/server/rest.js index fba4fca6..86ac0c57 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -20,6 +20,7 @@ pipe = main.pipe, CloudFunc = main.cloudfunc, dir = main.dir, + JSONDIR = main.JSONDIR, OK = 200, Header = main.generateHeaders({ name:'api.json' @@ -274,7 +275,8 @@ * @param pParams {command, method, body, requrest, response} */ function onPUT(pParams) { - var lRet = main.checkParams(pParams, ['body']); + var name, data, json, config, + lRet = main.checkParams(pParams, ['body']); if (lRet) { var p = pParams, @@ -350,6 +352,23 @@ main.sendError(pParams, p.data); break; + case 'config': + config = main.config; + + for (name in lFiles) + config[name] = lFiles[name]; + + json = Util.stringifyJSON(config) + '\n'; + + fs.writeFile(JSONDIR + 'config.json', json, function(error) { + if (error) + sendError(pParams, error); + else + sendMsg(pParams, 'config', name); + }); + + break; + default: send(pParams); break;