meteor-template-scope
Minimal replication of the angular's $scope mechanism.
Example:
In your markup,
<template name="foo"> Hello </template> <template name="bar"> {{> foo}} </template>
In your javascript,
1Template.bar.onCreated(function() { 2 this.new_scope = true; // Creates a new scope. 3 this.new_scope = false; // This or undefined uses parent's scope instead. 4 // Note: I really wanted to use this.scope, but apparently it was already used. 5 6 $(this).on('$scopeCreated', function() { 7 // this.$scope is now available. 8 }); 9}); 10 11Template.bar.onRendered(function() { 12 // Note: this.$scope is always available in onRendered. 13 14 $(this).on('$preLink', function() { 15 // Called while traversing downwards from root template 16 }); 17 18 $(this).on('$postLink', function() { 19 // Called while traversing updward from leaf template. 20 // Like a reverse DFS. Reverse $preLink. 21 }); 22}); 23 24Template.bar.onDestroyed(function() { 25 // No need to do destroy this.$scope, this will be handled. 26 // If you must destroy this.$scope prematurely, this.$scope.$destroy() 27 // will do the job. 28});
For the template above, the $preLink traversal is:
- bar
- foo
On the otherhand, the $postLink traversal is:
- foo
- bar
Development
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