meteoric124:template-scope

v0.1.0-beta.7Published 10 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

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:

  1. bar
  2. foo

On the otherhand, the $postLink traversal is:

  1. foo
  2. bar

Development

If you want to contribute, feel free to fork and make a pull request.

To run test locally:

  1. Add this to packages/ directory of some dummy meteor project.
  2. Execute: VELOCITY_TEST_PACKAGES=1 meteor test-packages --driver-package velocity:html-reporter jandres:template-scope