jandres:template-scope

Replication of the angular's $scope mechanism.


License
GPL-3.0
Install
meteor add jandres:template-scope@=0.1.0-beta.2

Documentation

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,

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:

  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