I want to create a module that improves the existing apostrophe-pieces for the purpose of translate pieces. So I make my module and I add a index.js file.
module.exports = {
improve: 'apostrophe-pieces',
alias:'apostrophe-translated-pieces',
construct: function(self, options) {
self.afterList = function(req, results, callback) {
console.log('my function');
return callback();
};
}
};
But the initial function is always called, and not mine. What is wrong with my approach?
There are two issues. First, it does not make sense to combine alias
with improve
. The purpose of improve
is to implicitly subclass apostrophe-pieces
so that all modules derived from apostrophe-pieces get your changes. The purpose of alias
is to give a module a new name and that is not what you are trying to do.
Second, improve
is really intended for npm modules, and does not currently work in "project level" code. You don't specify here but I assume you're doing this at project level, in lib/modules/apostrophe-translated-pieces
.
So how do you do this at project level? It's actually even simpler: you just create lib/modules/apostrophe-pieces/index.js
in your project and put your code there. If you supply an implementation for a module that is part of Apostrophe core, it is automatically loaded as an implicit subclass, doing exactly what you are hoping. You don't need the improve
keyword for that (or alias
for that matter).
The purpose of improve
is to let you pull off the same trick in an npm module you will publish and share with others, even though it can't (or shouldn't) have the same name as one of our modules. That issue doesn't come up at project level.
Finally... you should check out the apostrophe-workflow module. The new 2.x version of apostrophe-workflow also covers localization/translation and it does so very thoroughly. So it might not make sense to write your own implementation of translation, depending on your needs.