在节点中,我正在创建一个通过websockets连接到slack的模块实例。我用$.ajax({
url: "data.json",
//force to handle it as text
dataType: "text",
success: function(data) {
//data downloaded so we call parseJSON function
//and pass downloaded data
var json = $.parseJSON(data);
//Not sure what to do after this
}
});
执行此操作。如何进行设置以便在几个不同的模块中引用相同的const slackbot = new SlackBot()
?每次我想在模块中调用它时,最好的方法是将slackbot
传递给模块函数吗?
slackbot
答案 0 :(得分:0)
一种做法是
/** slacbot.js */
const slackbot = new SlackBot();
someModule.doThing(data, slackbot)
.then((response) => {
otherModule.doAnotherThing(response, slackbot);
})
.then((response) => {
someModule.wrapUp(response, slackbot);
});
// getter
var getSlackBot = function(){
if(slackBot) return slackBot
else {
slackbot = new SlackBot();
return slackbot;
}
}
module.exports.getSlackBot = getSlackBot;
/** consumer.js **/
var slackBot = require('slackBot').getSlackBot();