2
votes

I'm using couchdb 1.2.x, and I'm trying to use underscorejs in a list, without success.

here is how I proceed:

function(head, req) {
  var _ = require('vendor/underscore/underscore');
  log(_);
}

By looking at the couchdb log, I can see that var _ is undefined. Also, underscorejs log says:

1.3.0 — Jan. 11, 2012
Removed AMD (RequireJS) support from Underscore. If you'd like to use Underscore with RequireJS, you can load it as a normal script, wrap or patch your copy, or download a forked version.

I'm not sure exactly how to proceed; any clues?

Thanks

3

3 Answers

1
votes

The only suggest I have is to use the drop-in replacement Lo-Dash.

Among other things if offers AMD loader support.

1
votes

As the error message in the log states, Underscore.js is no longer formatted as an AMD module out of the box. You have two possible solutions: shim or use an AMD-ified underscore.

Shim:

Shim will wrap underscore in the necessary AMD boilerplate and be relatively transparent to you, allowing you to use an unmodified version of _.

In your case it's easy, configure requireJS like so:

require.config({
  paths: {
    underscore: "vendor/underscore/underscore"
  },
  shim: {
    underscore: {
      exports: '_'
    }
  }
});

Note: I also defined a path alias for underscore for convenience but it's not necessary to make the shim work.

AMD-ify:

An AMD-ified version of underscore (and backbone) is maintained by the author of requireJS here: https://github.com/amdjs/underscore

1
votes

Looks like you're trying to create a couchdb list function. Here's how I pulled in underscore. This is all in coffeescript...

In my design document I import and declare the library then pull that in from the list function:

designDoc =
  ...
  lib:
    underscore: "<actual underscore code as a string>"
  ...
  list:
    listFunction: "<list function as a string, see below>"
  ...

In the list function itself:

(head, req) ->
  _ = require 'lib/underscore'