Skip to content

Commit

Permalink
Merge branch 'templateEventsMethods' into devel
Browse files Browse the repository at this point in the history
Adds Template#onRendered, Template#onCreated and Template#onDestroyed methods
  • Loading branch information
Slava committed Jan 21, 2015
2 parents fb8a79b + 9c4e227 commit e2b78cf
Show file tree
Hide file tree
Showing 29 changed files with 244 additions and 191 deletions.
4 changes: 2 additions & 2 deletions docs/client/api-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ Template.apiBoxTitle.helpers({
}
});

Template.autoApiBox.rendered = function () {
Template.autoApiBox.onRendered(function () {
this.$('pre code').each(function(i, block) {
hljs.highlightBlock(block);
});
};
});

25 changes: 12 additions & 13 deletions docs/client/basic/sections/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,26 @@ event object and template instance. See the [Event Maps section](#eventmaps)
for details.
<!-- TODO Update the link to full docs for Event Maps -->

{{> autoApiBox "Template#rendered"}}
{{> autoApiBox "Template#onRendered"}}

The function assigned to this property is called once for every instance of
The functions added with this method are called once for every instance of
*Template.myTemplate* when it is inserted into the page for the first time.

This _rendered_ callback can be used to integrate external libraries that aren't
familiar with Meteor's automatic view rendering, and need to be initialized
every time HTML is inserted into the page. Use the
[`created`](#template_created) and
[`destroyed`](#template_destroyed) callbacks to perform
initialization or clean-up on any objects.
These callbacks can be used to integrate external libraries that
aren't familiar with Meteor's automatic view rendering, and need to be
initialized every time HTML is inserted into the page.
You can perform initialization or clean-up on any objects in
[`onCreated`](#template_oncreated) and [`onDestroyed`](#template_ondestroyed)
callbacks.

For example, to use the HighlightJS library to apply code highlighting to
all `<pre>` elements inside the `codeSample` template, you might assign
the following function to `Template.codeSample.rendered`:

<!-- XXX Why is this not a function like Meteor.startup? -->
```
Template.codeSample.rendered = function () {
Template.codeSample.onRendered(function () {
hljs.highlightBlock(this.findAll('pre'));
};
});
```

In the callback function, `this` is bound to a [template
Expand All @@ -257,8 +256,8 @@ You can assign additional properties of your choice to the template instance to
keep track of any state relevant to the template. For example, when using the
Google Maps API you could attach the `map` object to the current template
instance to be able to refer to it in helpers and event handlers. Use the
[`created`](#template_created) and [`destroyed`](#template_destroyed) callbacks
to perform initialization or clean-up.
[`onCreated`](#template_onCreated) and [`onDestroyed`](#template_onDestroyed)
callbacks to perform initialization or clean-up.

{{> autoApiBox "Blaze.TemplateInstance#findAll"}}

Expand Down
2 changes: 1 addition & 1 deletion docs/client/basic/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var sections = [
item("Defining templates in HTML", {id: "defining-templates"}),
item("Template.<em>name</em>.helpers", {longname: "Template#helpers"}),
item("Template.<em>name</em>.events", {longname: "Template#events"}),
item("Template.<em>name</em>.rendered", {longname: "Template#rendered"}),
item("Template.<em>name</em>.onRendered", {longname: "Template#onRendered"}),
item("<em>template</em>.findAll", {longname: "Blaze.TemplateInstance#findAll"}),
item("<em>template</em>.find", {longname: "Blaze.TemplateInstance#find"})
]
Expand Down
2 changes: 1 addition & 1 deletion docs/client/data.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/client/full-api/api/blaze.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ and manipulate "Views," the building blocks of reactive templates.

{{> autoApiBox "Blaze.render"}}

When you render a template, the
template's [`created`](#template_created) callback is invoked
immediately, before evaluating the content of the template.
The [`rendered`](#template_rendered) callback is
invoked after the View is rendered and inserted into the DOM.
When you render a template, the callbacks added with
[`onCreated`](#template_onCreated) are invoked immediately, before evaluating
the content of the template. The callbacks added with
[`onRendered`](#template_onRendered) are invoked after the View is rendered and
inserted into the DOM.

The rendered template
will update reactively in response to data changes until the View is
Expand All @@ -38,7 +38,7 @@ no longer needed.
Use `Blaze.remove` to remove a template or View previously inserted with
`Blaze.render`, in such a way that any behaviors attached to the DOM by
Meteor are cleaned up. The rendered template or View is now considered
["destroyed"](#template_destroyed), along with all nested templates and
["destroyed"](#template_onDestroyed), along with all nested templates and
Views. In addition, any data assigned via
jQuery to the DOM nodes is removed, as if the nodes were passed to
jQuery's `$(...).remove()`.
Expand Down Expand Up @@ -304,4 +304,4 @@ as well, but these objects are not yet part of the officially-supported,
public API.
{{/note}}

{{/template}}
{{/template}}
65 changes: 31 additions & 34 deletions docs/client/full-api/api/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,42 @@ To create a helper that can be used in any template, use
[`Template.registerHelper`](#template_registerhelper).


{{> autoApiBox "Template#rendered"}}
{{> autoApiBox "Template#onRendered"}}

This callback is called once when an instance of Template.*myTemplate* is
rendered into DOM nodes and put into the document for the first time.
Callbacks added with this method are called once when an instance of
Template.*myTemplate* is rendered into DOM nodes and put into the document for
the first time.

In the body of the callback, `this` is a [template
instance](#template_inst) object that is unique to this occurrence of
the template and persists across re-renderings. Use the `created` and
`destroyed` callbacks to perform initialization or clean-up on the
object.
In the body of a callback, `this` is a [template instance](#template_inst)
object that is unique to this occurrence of the template and persists across
re-renderings. Use the `onCreated` and `onDestroyed` callbacks to perform
initialization or clean-up on the object.

Because your template has been rendered, you can use functions like
[`this.findAll`](#template_findAll) which look at its DOM nodes.

{{> autoApiBox "Template#created"}}
{{> autoApiBox "Template#onCreated"}}

This callback is called before your template's logic is evaluated for the first
time. Inside the callback, `this` is the new [template
instance](#template_inst) object. Properties you set on this object will be
visible from the `rendered` and `destroyed` callbacks and from event handlers.
Callbacks added with this method called before your template's logic is
evaluated for the first time. Inside a callback, `this` is the new [template
instance](#template_inst) object. Properties you set on this object will be
visible from the callbacks added with `onRendered` and `onDestroyed` methods and
from event handlers.

This callback fires once and is the first callback to fire. Every
`created` has a corresponding `destroyed`; that is, if you get a
`created` callback with a certain template instance object in `this`,
you will eventually get a `destroyed` callback for the same object.
These callbacks fire once and are the first group of callbacks to fire.
Handling the `created` event is a useful way to set up values on template
instance that are read from template helpers using `Template.instance()`.

`created` is a useful way to set up values on template instance that are
read from template helpers using `Template.instance()`.
{{> autoApiBox "Template#onDestroyed"}}

{{> autoApiBox "Template#destroyed"}}

This callback is called when an occurrence of a template is taken off
These callbacks are called when an occurrence of a template is taken off
the page for any reason and not replaced with a re-rendering. Inside
the callback, `this` is the [template instance](#template_inst) object
a callback, `this` is the [template instance](#template_inst) object
being destroyed.

This callback is most useful for cleaning up or undoing any external effects of
`created` or `rendered`. It fires once and is the last callback to fire.
This group of callbacks is most useful for cleaning up or undoing any external
effects of `created` or `rendered` groups. This group fires once and is the last
callback to fire.


<h2 id="template_inst"><span>Template instances</span></h2>
Expand All @@ -92,19 +90,18 @@ the document. It can be used to access the DOM and it can be
assigned properties that persist as the template is reactively updated.

Template instance objects are found as the value of `this` in the
`created`, `rendered`, and `destroyed` template callbacks, and as an
`onCreated`, `onRendered`, and `onDestroyed` template callbacks, and as an
argument to event handlers. You can access the current template instance
from helpers using [`Template.instance()`](#template_instance).

In addition to the properties and functions described below, you can assign
additional properties of your choice to the object. Use the
[`created`](#template_created) and [`destroyed`](#template_destroyed) callbacks
to perform initialization or clean-up on the object.
[`onCreated`](#template_onCreated) and [`onDestroyed`](#template_onDestroyed)
methods to add callbacks performing initialization or clean-up on the object.

You can only access `findAll`, `find`, `firstNode`, and `lastNode`
from the `rendered` callback and event handlers, not from `created`
and `destroyed`, because they require the template instance to be
in the DOM.
You can only access `findAll`, `find`, `firstNode`, and `lastNode` from the
`onRendered` callback and event handlers, not from `onCreated` and
`onDestroyed`, because they require the template instance to be in the DOM.

Template instance objects are `instanceof Blaze.TemplateInstance`.

Expand Down Expand Up @@ -149,8 +146,8 @@ Access is read-only and non-reactive.

{{> autoApiBox "Blaze.TemplateInstance#autorun"}}

You can use `this.autorun` from a [`created`](#template_created) or
[`rendered`](#template_rendered) callback to reactively update the DOM
You can use `this.autorun` from a [`onCreated`](#template_onCreated) or
[`onRendered`](#template_onRendered) callback to reactively update the DOM
or the template instance. The Computation is automatically stopped
when the template is destroyed.

Expand Down
3 changes: 3 additions & 0 deletions docs/client/full-api/nameToId.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ nameToId = {
"Template#rendered": "template_rendered",
"Template#created": "template_created",
"Template#destroyed": "template_destroyed",
"Template#onRendered": "template_onRendered",
"Template#onCreated": "template_onCreated",
"Template#onDestroyed": "template_onDestroyed",
"Blaze.TemplateInstance#findAll": "template_findAll",
"Blaze.TemplateInstance#$": "template_$",
"Blaze.TemplateInstance#find": "template_find",
Expand Down
6 changes: 3 additions & 3 deletions docs/client/full-api/tableOfContents.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ var toc = [
{prefix: "Template", instance: "myTemplate", id: "templates_api"}, [
{name: "events", id: "template_events"},
{name: "helpers", id: "template_helpers"},
{name: "rendered", id: "template_rendered"},
{name: "created", id: "template_created"},
{name: "destroyed", id: "template_destroyed"}
{name: "onRendered", id: "template_onRendered"},
{name: "onCreated", id: "template_onCreated"},
{name: "onDestroyed", id: "template_onDestroyed"}
],
{name: "Template instances", id: "template_inst"}, [
{instance: "template", name: "findAll", id: "template_findAll"},
Expand Down
12 changes: 12 additions & 0 deletions docs/client/link-redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ Meteor.startup(function () {
return hash.replace("deps", "tracker");
}

if (hash.indexOf("_created") !== -1) {
return hash.replace("_created", "_onCreated");
}

if (hash.indexOf("_rendered") !== -1) {
return hash.replace("_rendered", "_onRendered");
}

if (hash.indexOf("_destroyed") !== -1) {
return hash.replace("_destroyed", "_onDestroyed");
}

if (hash === "meteor_collection") {
return "mongo_collection";
}
Expand Down
3 changes: 3 additions & 0 deletions docs/client/names.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@
"Template#destroyed",
"Template#events",
"Template#helpers",
"Template#onCreated",
"Template#onDestroyed",
"Template#onRendered",
"Template#rendered",
"Template.body",
"Template.currentData",
Expand Down
4 changes: 2 additions & 2 deletions examples/localmarket/client/templates/activity.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Template.activity.rendered = function() {
Template.activity.onRendered(function() {
var self = this;

// If the activity is in a list, scroll it into view. Note, we can't just use
Expand All @@ -11,7 +11,7 @@ Template.activity.rendered = function() {
var parentTop = $parent.offset().top;
$parent.scrollTop(top - parentTop);
}
}
});

Template.activity.helpers({
firstName: function() {
Expand Down
62 changes: 31 additions & 31 deletions examples/localmarket/client/templates/app-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,62 +52,62 @@ Meteor.startup(function () {
}, CONNECTION_ISSUE_TIMEOUT);
});

Template.appBody.rendered = function() {
Template.appBody.onRendered(function() {
this.find("#content-container")._uihooks = {
insertElement: function(node, next) {
// short-circuit and just do it right away
if (initiator === 'menu')
return $(node).insertBefore(next);

var start = (initiator === 'back') ? '-100%' : '100%';

$.Velocity.hook(node, 'translateX', start);
$(node)
.insertBefore(next)
.velocity({translateX: [0, start]}, {
duration: ANIMATION_DURATION,
easing: 'ease-in-out',
queue: false
});
.insertBefore(next)
.velocity({translateX: [0, start]}, {
duration: ANIMATION_DURATION,
easing: 'ease-in-out',
queue: false
});
},
removeElement: function(node) {
if (initiator === 'menu')
return $(node).remove();

var end = (initiator === 'back') ? '100%' : '-100%';

$(node)
.velocity({translateX: end}, {
duration: ANIMATION_DURATION,
easing: 'ease-in-out',
queue: false,
complete: function() {
$(node).remove();
}
});
.velocity({translateX: end}, {
duration: ANIMATION_DURATION,
easing: 'ease-in-out',
queue: false,
complete: function() {
$(node).remove();
}
});
}
};

this.find(".notifications")._uihooks = {
insertElement: function(node, next) {
$(node)
.insertBefore(next)
.velocity("slideDown", {
duration: ANIMATION_DURATION,
easing: [0.175, 0.885, 0.335, 1.05]
});
.insertBefore(next)
.velocity("slideDown", {
duration: ANIMATION_DURATION,
easing: [0.175, 0.885, 0.335, 1.05]
});
},
removeElement: function(node) {
$(node)
.velocity("fadeOut", {
duration: ANIMATION_DURATION,
complete: function() {
$(node).remove();
}
});
.velocity("fadeOut", {
duration: ANIMATION_DURATION,
complete: function() {
$(node).remove();
}
});
}
};
}
});


Template.appBody.helpers({
Expand Down
6 changes: 3 additions & 3 deletions examples/localmarket/client/templates/auth-overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
//
// We have to use an autorun for this as callbacks get lost in the
// redirect flow.
Template.authOverlay.created = function() {
Template.authOverlay.onCreated(function() {
this.autorun(function() {
if (Meteor.userId() && Overlay.template() === 'authOverlay')
Overlay.close();
});
}
});

Template.authOverlay.events({
'click .js-signin': function() {
Meteor.loginWithTwitter({loginStyle: 'redirect'});
}
});
});
Loading

0 comments on commit e2b78cf

Please sign in to comment.