代码之家  ›  专栏  ›  技术社区  ›  yusha uzumo

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

  •  0
  • yusha uzumo  · 技术社区  · 7 年前

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

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

    Pages = new Meteor.Pagination(PurchaseHistory, {
    	itemTemplate: "invoice",
    	availableSettings: {filters: true},
    	filters: {},
    	route: "/store/invoices/:_username/:startingDate/:endingDate/",
    	router: "iron-router",
    	routerTemplate: "invoices",
    	routerLayout: "main",
    	sort: {
    		transactionDate: 1
    	},
    	perPage: 1,
    	templateName: "invoices",
    	homeRoute:"home"
    });
    2 回复  |  直到 7 年前
        1
  •  0
  •   Gaëtan Rouziès    7 年前

    而不是向所有文档中添加新字段。您可以添加一个助手来加载公司文档:

    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>
    

        2
  •  0
  •   Sean    7 年前

    如果我理解正确的话,这听起来像是一份 composite publication .

    我以前使用过复合出版物来处理复杂的数据结构,它们工作得很好。