couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Hank Knight <hknight...@gmail.com>
Subject Re: CouchDB: JavaScript functions inside show functions
Date Sun, 19 Jan 2014 21:01:19 GMT
Great!  Is there a way for a JavaScript function to exist in one place
but to be available to ALL design documents?


On Sun, Jan 19, 2014 at 2:56 PM, Andy Wenk <andy@nms.de> wrote:
> 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
View raw message