Reputation: 3739
I have a class written in CoffeeScript:
class PresentationFramework
constructor: (framework) ->
log.info 'Looking for presentation framework module: ', framework
@template = fs.readFileSync path.join(framework, 'template.html'), 'utf8'
@renderer = handlebars.compile @template
@helpers = require path.join framework, 'helpers.js'
@render_deck = (data) =>
Object.keys(@helpers).forEach (key) ->
handlebars.registerHelper key, @helpers[key]
@renderer(data)
@
It compiles to the following JS:
PresentationFramework = (function() {
function PresentationFramework(framework) {
log.info('Looking for presentation framework module: ', framework);
this.template = fs.readFileSync(path.join(framework, 'template.html'), 'utf8');
this.renderer = handlebars.compile(this.template);
this.helpers = require(path.join(framework, 'helpers.js'));
this.render_deck = (function(_this) {
return function(data) {
return Object.keys(_this.helpers).forEach(function(key) {
handlebars.registerHelper(key, this.helpers[key]);
return this.renderer(data);
});
};
})(this);
this;
}
return PresentationFramework;
})();
I have written it with the intention of the following:
On creation of the object with the constructor (which is given a directory path), a couple of files are read and the contents assigned to fields of the class instance. In particular, the helpers field is assigned as a dictionary of functions imported from another file/module.
The render_deck method, is then supposed to go through the helpers dict of functions (iterate over the keys), and then it will register each function of the helpers dict with handlebars.
This is used in a nodejs script in the following way:
plugin = PresentationFramework(sessiondata.framework);
deck = plugin.render_deck(sessiondata);
However if I try this in node the console returns the following error:
/usr/users/TGAC_ga002/bward/github/slidewinder/lib/slidewinder_lib.js:78
handlebars.registerHelper(key, this.helpers[key]);
^
TypeError: Cannot read property 'slidewinder' of undefined
at /usr/users/TGAC_ga002/bward/github/slidewinder/lib/slidewinder_lib.js:78:56
at Array.forEach (native)
at PresentationFramework.render_deck (/usr/users/TGAC_ga002/bward/github/slidewinder/lib/slidewinder_lib.js:77:45)
at slidewinder (/usr/users/TGAC_ga002/bward/github/slidewinder/lib/slidewinder_lib.js:106:19)
at Object.<anonymous> (/usr/users/TGAC_ga002/bward/github/slidewinder/bin/slidewinder.js:31:3)
at Object.<anonymous> (/usr/users/TGAC_ga002/bward/github/slidewinder/bin/slidewinder.js:33:4)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
This is my first time writing anything serious in CoffeeScript and JS. I believe the issue is probably with how 'this' is used, I can see in the compiled JS, that the line:
handlebars.registerHelper(key, this.helpers[key]);
Should probably be:
handlebars.registerHelper(key, _this.helpers[key]);
Although I thought my use of the CoffeeScript '=>' should have prevented this.
Any pointers are greatly appreciated. Many Thanks.
Can anyone give me pointers on what the issue might be here?
Many Thanks.
Upvotes: 0
Views: 182
Reputation: 4515
probably Object.keys(@helpers).forEach (key) ->
switches context,
try Object.keys(@helpers).forEach (key) =>
Upvotes: 1