Question

CouchDB 1.1 comes with a very useful Javascript file called couch.js. I can locate it at http://localhost:5984/_utils/script/couch.js on my Couch instance.

I would like to use this script in a CouchApp. In particular I want to be able to run it from inside Javascript functions for update handlers and views. The file does not look CommonJS compliant, but I tried it anyway. I copied it to the lib directory of my couchapp. I tried to include it using CommonJS' require statement, but that fails to load. I tried the following:

var couchdb = require('lib/couch.js');

Can anybody help me to load it into a Javascript function? As I search the web I find help for including it in HTML documents, like so:

<script src="/_utils/script/couch.js"></script>

Obviously that will not work in my Javascript functions.

Also, if there is a way that I can include it from the _utils directory and not have to copy it into my couchapp lib directory, I would prefer to do that. But that is just an extra bonus.

Was it helpful?

Solution

While you could add the exports method to make it CommonJS compliant, you would not want to use couch.js inside your views and update handlers. The couch.js file is designed to run inside a javascript environment that supports ajax. The couchdb javascript environment does not expose any ajax functionality.

OTHER TIPS

For it you will need to change the couch.js's source and add a exports.CouchDB = CouchDB; at the end of the file (check the license before doing that. I'm not a lawyer). Than after doing var couchdb = require('lib/couch.js'); in your code just do var somevarname = new couchdb.CouchDB(/*parameters*/);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top