From f146489c271aabdb9d7ccb01707682dae08146cf Mon Sep 17 00:00:00 2001 From: Slava Kim Date: Wed, 21 Jan 2015 12:26:51 -0800 Subject: [PATCH] Fix the bug of firing a rendered callback when it was added too late --- packages/blaze/template.js | 14 ++++++++++---- packages/spacebars-tests/template_tests.js | 5 ++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/blaze/template.js b/packages/blaze/template.js index 6796890d51..9048eff84b 100644 --- a/packages/blaze/template.js +++ b/packages/blaze/template.js @@ -98,14 +98,17 @@ Template.prototype.onDestroyed = function (cb) { this._callbacks.destroyed.push(cb); }; -Template.prototype._fireCallbacks = function (template, which) { +Template.prototype._getCallbacks = function (which) { var self = this; var callbacks = self[which] ? [self[which]] : []; // Fire all callbacks added with the new API (Template.onRendered()) // as well as the old-style callback (e.g. Template.rendered) for // backwards-compatibility. callbacks = callbacks.concat(self._callbacks[which]); + return callbacks; +}; +var fireCallbacks = function (callbacks, template) { for (var i = 0, N = callbacks.length; i < N; i++) { callbacks[i].call(template); } @@ -177,8 +180,9 @@ Template.prototype.constructView = function (contentFunc, elseFunc) { * @locus Client * @deprecated in 1.1 */ + var createdCallbacks = self._getCallbacks('created'); view.onViewCreated(function () { - self._fireCallbacks(view.templateInstance(), 'created'); + fireCallbacks(createdCallbacks, view.templateInstance()); }); /** @@ -189,8 +193,9 @@ Template.prototype.constructView = function (contentFunc, elseFunc) { * @locus Client * @deprecated in 1.1 */ + var renderedCallbacks = self._getCallbacks('rendered'); view.onViewReady(function () { - self._fireCallbacks(view.templateInstance(), 'rendered'); + fireCallbacks(renderedCallbacks, view.templateInstance()); }); /** @@ -201,8 +206,9 @@ Template.prototype.constructView = function (contentFunc, elseFunc) { * @locus Client * @deprecated in 1.1 */ + var destroyedCallbacks = self._getCallbacks('destroyed'); view.onViewDestroyed(function () { - self._fireCallbacks(view.templateInstance(), 'destroyed'); + fireCallbacks(destroyedCallbacks, view.templateInstance()); }); return view; diff --git a/packages/spacebars-tests/template_tests.js b/packages/spacebars-tests/template_tests.js index f89a89c13a..e8df51753d 100644 --- a/packages/spacebars-tests/template_tests.js +++ b/packages/spacebars-tests/template_tests.js @@ -1252,9 +1252,12 @@ Tinytest.add('spacebars-tests - template_tests - inclusion helpers are isolated' }}); var div = renderToDiv(tmpl); - subtmplCopy.rendered = function () { + subtmplCopy.rendered = function () { test.fail("shouldn't re-render when same value returned from helper"); }; + subtmplCopy.onRendered(function () { + test.fail("shouldn't re-render when same value returned from helper"); + }); dep.changed(); Tracker.flush({_throwFirstError: true}); // `subtmplCopy.rendered` not called