Skip to content

Commit 2152734

Browse files
committed
fixes adding a component that has already been torn down
1 parent 9e78bd8 commit 2152734

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

can-component.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ var Component = Construct.extend(
339339
// ### setup
340340
// When a new component instance is created, setup bindings, render the view, etc.
341341
setup: function(el, componentTagData) {
342+
this._initialArgs = [el,componentTagData];
342343
var component = this;
343344
var options = {
344345
helpers: {},
@@ -550,6 +551,7 @@ var Component = Construct.extend(
550551

551552
// Keep a nodeList so we can kill any directly nested nodeLists within this component
552553
var nodeList = nodeLists.register([], function() {
554+
component._torndown = true;
553555
domEvents.dispatch(el, "beforeremove", false);
554556
if(teardownBindings) {
555557
teardownBindings();
@@ -585,11 +587,15 @@ var Component = Construct.extend(
585587
}
586588

587589
}
590+
component._torndown = false;
588591
}
589592
});
590593

591594
// This adds support for components being rendered as values in stache templates
592595
Component.prototype[viewInsertSymbol] = function(viewData) {
596+
if(this._torndown) {
597+
this.setup.apply(this,this._initialArgs);
598+
}
593599
viewData.nodeList.newDeepChildren.push(this.nodeList);
594600
return this.element;
595601
};

test/component-in-stache-test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ QUnit.test("wrapped in a conditional", function (assert) {
7777
assert.equal(fragment.textContent, "", "fragment ends without content");
7878
});
7979

80-
QUnit.test("Component can be removed from the page", 2, function(){
80+
QUnit.test("Component can be removed from the page", 3, function(){
8181

8282
var ToBeRemoved = Component.extend({
8383
tag: "to-be-removed",
@@ -104,11 +104,16 @@ QUnit.test("Component can be removed from the page", 2, function(){
104104

105105
var template = stache("<div>{{# if(show) }} {{{toBeRemoved}}} {{/ if}}</div>");
106106

107-
template({
107+
var frag = template({
108108
show: show,
109109
toBeRemoved: toBeRemoved
110110
});
111111

112112
show.set(false);
113113
QUnit.ok(true, "got here without an error");
114+
115+
show.set(true);
116+
117+
prop.set(4);
118+
QUnit.equal(frag.firstChild.getElementsByTagName("to-be-removed")[0].innerHTML, "4");
114119
});

0 commit comments

Comments
 (0)