在onBefore钩子或类似钩子中更改返回的集合

问题描述:

是否可以在onBeforeAction或类似钩子中更改(为每个记录添加更多字段)返回的集合?在onBefore钩子或类似钩子中更改返回的集合

我有InvoiceHistory集合,我通过分页。我还想在每张发票中显示公司名称,公司地址,电子邮件地址和增值税登记号码 - 这四个字段存储在另一个收藏中。所以我想将这四个字段添加到从InvoiceHistory返回的每个记录中。如果还有其他方法可以做到这一点,我乐于接受建议。当您导航/浏览/翻页到第二页时,我正在使用Alethes meteor分页,它丢失了itemTemplate中返回的帮助字段。 Alethes分页还依赖于铁路由器,是否有可能通过铁路路由器实现我想要的功能

===================== ================================================== =

@Sean。谢谢。下面是使用流星发布复合代码:

if (Meteor.isServer) { 
 

 
\t import { publishComposite } from 'meteor/reywood:publish-composite'; 
 

 
\t publishComposite('invoicesWithCompanyDetails', function(userId, startingDate,endingDate) { 
 
\t \t return { 
 
\t \t \t find() { 
 
\t \t \t \t // Find purchase history for userId and the two dates that were entered. Note arguments for callback function 
 
\t \t \t \t // being used in query. 
 
\t \t \t \t return PurchaseHistory.find({Id:userId,transactionDate:{$gte:new Date(decodeURIComponent(startingDate)), 
 
\t \t \t \t \t $lt:new Date(decodeURIComponent(endingDate))}}); 
 
\t \t \t }, 
 
\t \t \t children: [ 
 
\t \t \t \t { 
 
\t \t \t 
 
\t \t \t \t \t find() { 
 
\t \t \t \t \t \t return CompanySharedNumbers.find(
 
\t \t \t \t \t \t \t { _id:"firstOccurrence" }, 
 
\t \t \t \t \t \t \t { fields: { companyName: 1, companyAddress: 1 } } 
 
\t \t \t \t \t \t); 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t ] 
 
\t \t } 
 
\t }); 
 
}

Router.route('/store/invoices/:_username/:startingDate/:endingDate', { //:_startingDate/:_endingDate 
 
\t name: 'invoices', 
 
\t template: 'invoices', 
 
\t onBeforeAction: function() 
 
\t { 
 
\t \t ... 
 
\t }, 
 
\t waitOn: function() { 
 
\t \t var startingDate = this.params.startingDate; 
 
\t \t var endingDate = this.params.endingDate; 
 
\t \t return [Meteor.subscribe('systemInfo'),Meteor.subscribe('testingOn'),Meteor.subscribe('invoicesWithCompanyDetails',startingDate,endingDate)]; 
 
\t } 
 
});

Pages = new Meteor.Pagination(PurchaseHistory, { 
 
\t itemTemplate: "invoice", 
 
\t availableSettings: {filters: true}, 
 
\t filters: {}, 
 
\t route: "/store/invoices/:_username/:startingDate/:endingDate/", 
 
\t router: "iron-router", 
 
\t routerTemplate: "invoices", 
 
\t routerLayout: "main", 
 
\t sort: { 
 
\t \t transactionDate: 1 
 
\t }, 
 
\t perPage: 1, 
 
\t templateName: "invoices", 
 
\t homeRoute:"home" 
 
});

+0

看看集合转换https://docs.meteor.com/api/collections.html也,你可能更喜欢denormalizing ... – Salketer

+0

我看了看转换;整洁,但你如何将它应用到alethes分页集合?反规范化肯定会起作用,但是我正在构建的应用程序已经需要大量空间,我正在尽可能地提高效率。 –

而不是将新字段添加到您的所有文档。你可以做的是增加一个助手来加载一份公司文件:

Template.OneInvoice.helpers({ 
    loadCompany(companyId){ 
     return Company.findOne({_id: companyId}); 
    } 
}); 

而在你的模板代码使用它:

<template name="OneInvoice"> 
    <p>Invoice id: {{invoice._id}}</p> 
    <p>Invoice total: {{invoice.total}}</p> 
    {{#let company=(loadCompany invoice.companyId)}} 
     <p>Company name: {{company.name}}</p> 
     <p>Company business address: {{company.businessAddress}}</p> 
    {{/let}} 
</template> 

这样,代码很简单,易于维护。

+0

我已经尝试了你的建议。看到我上面的评论:“我使用Alethes流星分页,当你导航/浏览/页面到第二页时,失去了itemTemplate中返回的帮助字段”如果我需要它在一页上,这将是一个很好的解决方案。尽管谢谢你的回答。 –

+0

我甚至在重新查看代码后给出了let语句。它只显示在第一页上,就像我以前一样。对不起,再次感谢。 –

+1

**所以为了避免重复已经写在这个页面上的内容,@Gaetan在这里部分是正确的。唯一缺少的是,包含公司详细信息的集合的订阅不能位于特定的铁路由器路由上,而是全局的**所以在我的情况下,Meteor.subscribe('companyAddressNameEmailAndVatNumber')必须是全局的 –

如果我正确理解你,这听起来像是一个composite publication的工作。

使用复合发布,您可以同时返回多个集合中的相关数据。

因此,您可以找到所有发票,然后使用存储在发票对象中的公司ID,可以同时从公司集合中返回公司名称和其他信息。

我以前使用过复合出版物来处理复杂的数据结构,并且它们完美地工作。

希望有帮助

+0

看起来可能会有效。我会尝试并让你知道。我想鼓励其他人提出建议,这就是为什么我没有早点评论的原因 –

+0

哦,顺便其他人你的建议仍然欢迎 –

+0

我试过你的建议。这些页面不显示公司的详细信息。请参阅我的代码 –