문제

I'm refactoring a javascript codebase and am implementing, but I'm new to node. I might run into some code like this:

foo.js

var foo = {};
foo.bar = function(baz) {
    $('body').append(baz)
}

which i would then refactor into the following:

foo.js

var $ = require('jquery')(window);
var foo = {};
foo.bar = require('./bar');

bar.js

module.exports = bar = function(baz) {
    $('body').append(baz);
}

What's the correct way to pass the jQuery object from foo.js to bar.js without interfering with the baz parameter when foo.bar(baz) is called?

도움이 되었습니까?

해결책

Just add var $ = require('jquery')(window) to each module that needs jQuery!

Calls to require that resolve to the same path will return a cached copy of the module:

http://nodejs.org/api/modules.html#modules_caching

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top