From 3a08b41f8c2a97eee0904b6359e559ed76dfc9fd Mon Sep 17 00:00:00 2001 From: Christopher Lenz Date: Tue, 20 May 2008 20:15:05 +0000 Subject: Some javascript code cleanup. git-svn-id: https://svn.apache.org/repos/asf/incubator/couchdb/trunk@658409 13f79535-47bb-0310-9956-ffa450edef68 --- share/www/script/couch.js | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) (limited to 'share/www/script/couch.js') diff --git a/share/www/script/couch.js b/share/www/script/couch.js index d63f1eae..0b2f828c 100644 --- a/share/www/script/couch.js +++ b/share/www/script/couch.js @@ -94,12 +94,19 @@ function CouchDB(name) { } // Applies the map function to the contents of database and returns the results. - this.query = function(mapFun, options) { + this.query = function(mapFun, reduceFun, options) { + var body = {language: "javascript"}; if (typeof(mapFun) != "string") mapFun = mapFun.toSource ? mapFun.toSource() : "(" + mapFun.toString() + ")"; + body.map = mapFun; + if (reduceFun != null) { + if (typeof(reduceFun) != "string") + reduceFun = reduceFun.toSource ? reduceFun.toSource() : "(" + reduceFun.toString() + ")"; + body.reduce = reduceFun; + } var req = request("POST", this.uri + "_temp_view" + encodeOptions(options), { headers: {"Content-Type": "application/json"}, - body: JSON.stringify({language:"javascript",map:mapFun}) + body: JSON.stringify(body) }); var result = JSON.parse(req.responseText); if (req.status != 200) @@ -107,22 +114,6 @@ function CouchDB(name) { return result; } - // Applies the map function to the contents of database and returns the results. - this.reduce_query = function(mapFun, reduceFun, options) { - if (typeof(mapFun) != "string") - mapFun = mapFun.toSource ? mapFun.toSource() : "(" + mapFun.toString() + ")"; - if (typeof(reduceFun) != "string") - reduceFun = reduceFun.toSource ? reduceFun.toSource() : "(" + reduceFun.toString() + ")"; - var req = request("POST", this.uri + "_temp_view" + encodeOptions(options), { - headers: {"Content-Type": "application/json"}, - body: JSON.stringify({language:"javascript",map:mapFun,reduce:reduceFun}) - }); - var result = JSON.parse(req.responseText); - if (req.status != 200) - throw result; - return result; - } - this.view = function(viewname, options) { var req = request("GET", this.uri + "_view/" + viewname + encodeOptions(options)); if (req.status == 404) -- cgit v1.2.3