For a web-app I’m building, I’m really interested in recursing through an object tree and modifying nodes on the fly. There’s an underscore mixin that promises that, underscore.loop, but I can’t seem to pull it into my page. I get a script error:
Uncaught TypeError: Cannot call method ‘mixin’ of undefined
which is on line 33 of the tool.
Now, underscore.loop.js is pulled in after backbone.js and underscore-data.js.
- Both of these tools use Underscore, and
-
Underscore is definitely available after trying
underscore.loop.
So why can’t underscore.loop.js see Underscore. It uses pretty much the same scoping and initialization semantics as underscore-data.js. Can anyone shed any light on this?
Look a little higher in the source for underscore.loop and you’ll see this:
Note the
var ... _;and how_is initialized. Sounderscore.loop.jsis trying to make sure thatunderscore.jsis loaded and that it has a local version of_to use.requireis a node.js-ism so you don’t have it in your client-side world and that leaves you with an undefined value in_. You can either grab a client-side library that supplies a node.js compatible require:or edit your copy of
underscore.loop.jsto not include thevar _;declaration or thetryblock. Alternatively, you could hack up a littlerequireimplementation of your own that just does this:or even:
and load your
requirehack betweenunderscore.jsandunderscore.loop.js.