Meteor: how to access parent properties within nested templates?

前端 未结 3 1480
慢半拍i
慢半拍i 2020-12-15 09:58

I am getting started with Meteor, and adapting the todo example to include nested tag groups. I have the following HTML, which outputs each name of each tag group, plus the

相关标签:
3条回答
  • 2020-12-15 10:38
    "click selected":function(e){
        // this._id
       var doc_id = $(e.currentTarget).parent().parent().attr("uid")
       console.log(doc_id) 
    },
    //specify the each id in the div above the nearest #each
    //this will work in events but not in helpers`
    
    0 讨论(0)
  • 2020-12-15 10:45

    I'm not sure whether you can get parent template data, but in your event handler you can access DOM elements: event.currentTarget will get the clicked element. Then just use jQuery to access attributes. If needed, event.currentTarget.parentNode will get the parent element in the DOM.

    Ex: I don't know where you put the tag class, but let's say it's the class of your div where data-taggroup is defined. Then you could get the tag name using:

    $(event.currentTarget).attr('data-taggroup')
    
    0 讨论(0)
  • 2020-12-15 10:53

    I found the solution to access parent data:

    Template.nestedTemplate.events({
        'click a.class':function(event,template){
            var parentID = template.data._id;
            console.log(parentID);
        }
    });
    

    The .events handler function receives two arguments: event, an object with information about the event, and template, a template instance for the template where the handler is defined. Took me a really long time to figure this one out. Don't use the handlebars solution, it shows your data!

    0 讨论(0)
提交回复
热议问题