在我的项目中,其中一个模板有onRendered
方法超过250行。代码变得越来越不可读和不可维护的(其单片因为)我想分割的所有代码实现的功能是这样的:如何将代码拆分为流星模板中的函数
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()) {
...
}
});
}
当然,我可以在onRendered
法定义的功能,但声明他们在onRendered
和在同一个地方使用似乎不是完美的方式做到这一点 - 我正在寻找像从模板中移动功能,甚至将它们移动到另一个文件。你能否告诉我一种实现这个目标的方法?
你使用什么版本的流星? – Calvin
@Calvin流星1.3 – getupandgo