Minimal replication of the angular's $scope mechanism.
In your markup,
<template name="foo">
Hello
</template>
<template name="bar">
{{> foo}}
</template>
In your javascript,
Template.bar.onCreated(function() {
this.new_scope = true; // Creates a new scope.
this.new_scope = false; // This or undefined uses parent's scope instead.
// Note: I really wanted to use this.scope, but apparently it was already used.
$(this).on('$scopeCreated', function() {
// this.$scope is now available.
});
});
Template.bar.onRendered(function() {
// Note: this.$scope is always available in onRendered.
$(this).on('$preLink', function() {
// Called while traversing downwards from root template
});
$(this).on('$postLink', function() {
// Called while traversing updward from leaf template.
// Like a reverse DFS. Reverse $preLink.
});
});
Template.bar.onDestroyed(function() {
// No need to do destroy this.$scope, this will be handled.
// If you must destroy this.$scope prematurely, this.$scope.$destroy()
// will do the job.
});
For the template above, the $preLink traversal is:
- bar
- foo
On the otherhand, the $postLink traversal is:
- foo
- bar
If you want to contribute, feel free to fork and make a pull request.
To run test locally:
- Add this to packages/ directory of some dummy meteor project.
- Execute:
VELOCITY_TEST_PACKAGES=1 meteor test-packages --driver-package velocity:html-reporter jandres:template-scope