Meteor: Is there a reactive per-template scope?

后端 未结 4 1638
难免孤独
难免孤独 2020-12-25 15:23

I\'m rendering the same Handlebars template in multiple (arbitrarily many) locations on the same page. Inside each template, I want a button to toggle the visibility of a di

4条回答
  •  执念已碎
    2020-12-25 15:52

    I think that discussion on meteor-core is outdated (though it is recent). Per the docs (quoted below), this should be possible:

    Template.myTemplate.events({
       foo: function(event, template) {
          var _div = template.find('...');
       }
    });
    

    The 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. The handler also receives some additional context data in this, depending on the context of the current element handling the event. In a Handlebars template, an element's context is the Handlebars data context where that element occurs, which is set by block helpers such as #with and #each.

    Update

    Assuming each hideable div had a unique id, what if you tried something like this? I haven't tested it.

    Template.jsonObject.hidden = function(){
        var _id = $(".classOfPossibleHiddenDiv").prop("id");
        return Session.get(_id) || false;
    };
    
    Template.jsonObject.events({
        'click input' : function(event, template){
            Session.set($(template.find(".classOfPossibleHiddenDiv")).prop("id"), true);
        }
    });
    

提交回复
热议问题