Accessing parent context in Meteor templates and template helpers

前端 未结 9 915
别跟我提以往
别跟我提以往 2020-12-09 01:10

I\'m running into a template context situation that I\'m having a hard time finding a way around.


Here\'s the template in question:



        
9条回答
  •  孤城傲影
    2020-12-09 01:31

    It's not particularly pretty, but I've done something like this:

    
    
    
    
    
    // and in the js:
    Template.forLoop.helpers({
        augmentedParticipants: function() {
            var self = this;
            return _.map(self.participants,function(p) {
                p.parent = self;
                return p;
            });
        }
    });
    

    It's similar to the approach that AVGP suggested, but augments the data at the helper level instead of the db level, which I think is a little lighter-weight.

    If you get fancy, you could try to write a Handlebars block helper eachWithParent that would abstract this functionality. Meteor's extensions to handlebars are documented here: https://github.com/meteor/meteor/wiki/Handlebars

提交回复
热议问题