Created
August 18, 2015 08:29
-
-
Save namirsab/de6624dd7550d0d11fa8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template name="parent"> | |
| {{>Template.contentBlock}} | |
| </template> | |
| <template name="child1"> | |
| {{#parent}} | |
| <div class="whateverDiv"> | |
| <a href="#">{{parentHelper}}</a> | |
| </div> | |
| {{/parent}} | |
| </template> | |
| <template name="child2"> | |
| {{#parent}} | |
| <span class="whatever"> {{parentHelper}} </span> | |
| {{/parent}} | |
| </template> | |
| The problem you have with this is that you can't access to parent helpers from the block template. | |
| What I did is this: | |
| var helpers = { | |
| parentHelper: function () { | |
| var data = fetchDataFromSomeWhere(); | |
| return data; | |
| } | |
| }; | |
| Template.parent.onCreated(function () { | |
| Template.parent.__liveInstances = Template.parent.__liveInstances || 0; | |
| Template.parent.__liveInstances++; | |
| //Here you can do whatever is common for all the child templates, subscribing to a publication, etc. | |
| //Register global helpers as local helpers | |
| if (Template.parent.__liveInstances > 0) { | |
| _.each(_.keys(helpers), function (helperName) { | |
| Template.registerHelper(helperName, helpers[helperName]); | |
| }); | |
| } | |
| }); | |
| Template.parent.onDestroyed(function () { | |
| //"Unregister" helpers | |
| Template.projectListInfo.__liveInstances--; | |
| if (Template.projectListInfo.__liveInstances === 0) { | |
| _.each(_.keys(helpers), function (helperName) { | |
| Template.registerHelper(helperName, _.noop); | |
| }); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment