PDA

View Full Version : JS: require in addons? (IDE 9)



JasonRogers
10-02-2015,
In this wiki page
5 (https://github.com/Komodo/KomodoEdit/wiki/Komodo-9-Require) I see that addons can require custom js files.
Let's imagine the situation:
In file "mycooljs.js" in folder content/js/ I have this code:
var myobj = {
name: "test",
func: function () {
return name in this;
}
}And in chrome.manifest I insert this line:
category require-path mycooljs chrome://myaddon/content/js/And in another js file (e.g. "myanothercooljsfile.js") I try to require mycooljs:
var mycooljs = require("myaddon/mycooljs");And what I get in mycooljs variable? Can I get access to object myobj in mycooljs.js via mycooljs.myobj?

JeelaMartin
10-03-2015,
You have to tell commonjs require what you want to expose, eg. by setting module.exports = myobj.
Sample usage:
// Module
var myobj = {
name: "test",
func: function () {
return name in this;
}
}

module.exports = myobj;

// Usage
require('myaddon/mycooljs').func();

Jeanyopupdam
10-04-2015,
Okay, but if I have more that 1 object? E.g.
var myobj = {
name: "test",
func: function () {
return name in this;
}
}
var myobj2 = {
callback: false,
callback_func: function callback_func(callback, statement) {
//code
}
}I must write module.exports = myobj; module.exports = myobj2; or module.exports = {myobj, myobj2}; or module.exports = [myobj, myobj2]?

JemFevzi
10-06-2015,
module.exports = {
myobj: ...
myobj2: ...
}or
var myobj = {};
var myobj2 = {};
module.exports = {myobj: myobj, myobj2: myobj2};Basically you can do whatever you want, just make sure that any data you want to expose is in module.exports