couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andy Wenk <a...@nms.de>
Subject Re: CouchDB: JavaScript functions inside show functions
Date Sun, 19 Jan 2014 18:56:44 GMT
On 19 January 2014 18:35, Hank Knight <hknight555@gmail.com> wrote:

> How can I add a JavaScript function to a show function?
> 'XYZ: '+ (111*2)
>
> I would like to reference a custom function like this:
> 'XYZ: '+ zooph(111)
>
> This example does NOT work:
>
> "myshowfunction": "function(doc, req){ return{'headers':
> {'Content-Type' : 'text/plain'}, 'body': function zooph(x) {return
> x*2;} + 'XYZ: '+ zooph(111)}}"
>

you can add a commonJS module to a design document. It is available within
the whole design document and you can reference it from your show function.
here is an example:
{
  "_id": "_design/somename",
  "_rev": "275-a3f8495cc032a09fb1dfdbaa26da0703",
  "views": {
    "lib": {
      "multi2": "function(x) { return x * 2; }",
      "multi4": "function(x) { return x * 4; }"
      }
    }
  }
}

You would then require the module in your show function with

require('lib/multi2') or require('lib/multi4').

You could also namespace this in the code above with

 "lib": {
    "modulenamesapce": {
        "multi2": "function(x) { return x * 2; }",
        "multi4": "function(x) { return x * 4; }"
      }
    }
 }

and require it respectively.

Please refer also to
http://docs.couchdb.org/en/latest/query-server/javascript.html?highlight=commonjs#commonjs-modules

Cheers

Andy

-- 
Andy Wenk
Hamburg - Germany
RockIt!

http://www.couchdb-buch.de
http://www.pg-praxisbuch.de

GPG fingerprint: C044 8322 9E12 1483 4FEC 9452 B65D 6BE3 9ED3 9588

https://people.apache.org/keys/committer/andywenk.asc

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message