Search code examples
javascripttemplatesmeteorecmascript-6meteor-blaze

How to refer parent template in quickform attribute _id


Consider Below Code.

{{#each assignments}}
  {{#with eachClientDetails}}
    {{#quickRemoveButton collection=assignment _id=this._id }}
       Delete
    {{/quickRemoveButton}}
  {{/with}}
{{/each}}

In above code, I am iterating each assignment and each assignment has single Client Detail. With each Client Detail I am adding Delete button.

Helper:

eachClientDetails(){
    var client = Clients.find({_id: this.clientId}).fetch()[0];
    console.log(client);
    return client;
}

But the problem is, while assigning attributes to _id of quickForm, I can only assign data from current context(i.e. this._id). All I need is to access context of assignment (desired like _id=../_id). But I get below error,

Can only use `this` at the beginning of a path.
Instead of `foo.this` or `../this`, just write `foo` or `..`.

Is it possible using any helper and stuff to get the parent templates _id


Solution

  • Your issue with overwriting contexts could be solved by using #each in and #let instead of #each and #with:

    {{#each assignment in assignments}}
      {{#let client=(eachClientDetails assignment)}}
        {{#quickRemoveButton collection=assignment _id=assignment._id }}
           Delete {{client.name}}
        {{/quickRemoveButton}}
      {{/let}}
    {{/each}}
    

    Here, {{client.name}} has been added just to show how to access client's fields.

    And helper's code:

    eachClientDetails(assignment){
        var client = Clients.findOne({_id: assignment.clientId});
        console.log(client);
        return client;
    }