Reputation: 105
In my project one of the templates has in it's onRendered
method more than 250 lines. Code is becoming more and more unreadable and unmaintainable (because of its monolithic) and i want to split all the code to functions to achieve something like this:
Template.Products.onRendered(function () {
initCarousels();
const allProducts = Meteor.call('server/collections/products::getAll', product._id, (err, content) => {
...
});
const sortedProducts = sortProducts(allProducts);
updateCarousels(sortedProducts);
...
this.autorun( () => {
this.subscribe('products');
...
if (this.subscriptionsReady()) {
...
}
});
}
Of course, i can define functions in onRendered
method, but declaring them in onRendered
and using in the same place seems not perfect way do do this - i'm searching something like moving functions from templates or even moving them to another file. Can you advise me a way to achieve this?
Upvotes: 1
Views: 104
Reputation: 6018
Yes! you can move code to lib/ directory and use which will be sibling of Client and server folders.
Template.registerHelpers(function_name, function());
When the server starts it will first load lib/*.js files alphabetically, so you can use the files from client.
Upvotes: 2