I have been searching on the Internet and couldn’t find anything. Is there an elegant way to find a Template instance and get it’s data context outside of that template? I am currently doing that, this event listener is on my body wrapper:
'click [data-ion-radio]': function(event) {
// Get template name for Blaze.render
var templateName = $(event.currentTarget).data('ion-radio');
// Get the rest of attributes from the button
var data = $(event.currentTarget).data();
// This is my array of objects that I need to pass as data, I am currently
passing id of a wrapper of my template that holds the required data
var data.radio = Blaze.getView($('#'+data.template)[0]).templateInstance().data.radio;
IonRadioSheet.open(templateName, data);
}
**** data.template = 'wrapper-id' as per sample template below ****
So this forces me to the following template syntax for the template with data
<template name="myTemplate">
<div id="wrapper-id">
CONTENT
</div>
</template>
Any other way to get that data without having to wrap my template content? If I call Blaze.getView on event.currentTarget I have to get to the parent, but the number of levels I have to go up will differ throughout the app.
Thanks for feedback! The problem is that the template passed onto my event handler is the main parent (body element). It is not the template that contains my data, thus I would still have to look for the child template by name.
Well, the current data context in event helper should be “this” by default.
Or check the todo example, how it is used there
So console.log(this);
And check it in browser console.
If it does not work, you can still do someting like Blaze.getData(e.currentTarget);
Unfortunately ‘this’ is not the template I am looking for. I would hope there was a simple Blaze API that allows Template instance access. What is in my primary post is the only thing that have worked for my use case, however I would want to avoid an extra wrapper in my template just for the sake of this working.
Still if you better describe what you are trying to code, we will 99% find better solution how to do it.
Not just that template up there, but what u want to accomplish from user story perspective.
Thanks a lot, template.get() and template.parent() were exactly what I was looking for. I was already using kind of a parent template loop, but haven’t thought of skipping (contentBlock) templates like this one does.
And what I was trying to achieve - I needed a modal with selectable list items generated from the data of current active template. However, my event handler is global, the list ‘modal’ will be called in variety of places including other modals and templates. My primary obstacle was that when trying to access parent templates of ‘event.currentTarget’ I would get (contentBlock) template, and the number of levels to go up to get the data would differ from template to template.
If a second argument is passed toModal.show (Modal.show(<templateName>, <dataContext>)), it will be used as the data context for the template (works as the data parameter for Blaze.renderWithData).
So I would just use this modal extension and pass it as data object which would 1 property for example options: ourArrayOfOptions
So u would be able to reuse that modal with xyz different options, combinations and list of options
Though low level API with direct access to a Template instance would be really great instead of having to loop through parents in search for template instance with specified field (via Blaze.TemplateInstance.prototype.get from that package).
I agree, also a valid solution, although I wanted to avoid global variables and having to assign the link from every template. Thanks again for pointing me in the right direction