Fix the bug of firing a rendered callback when it was added too late

This commit is contained in:
Slava Kim
2015-01-21 12:26:51 -08:00
parent e2b78cfd0b
commit f146489c27
2 changed files with 14 additions and 5 deletions

View File

@@ -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;

View File

@@ -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