Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
add Template.prototype.clear
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelw committed Feb 5, 2014
1 parent a07d1c6 commit cc00394
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/TemplateBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,17 @@
return this.delegate_ && this.delegate_.raw;
},

clear: function() {
this.model_ = undefined;
this.delegate_ = undefined;
this.bindings_ = undefined;
if (!this.iterator_)
return;
this.iterator_.valueChanged();
this.iterator_.close()
this.iterator_ = undefined;
},

setDelegate_: function(delegate) {
this.delegate_ = delegate;
this.bindingMap_ = undefined;
Expand Down
28 changes: 28 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,34 @@ suite('Template Instantiation', function() {
});
});

test('Template.clear', function(done) {
var div = createTestHtml(
'<template repeat>{{}}</template>');
var template = div.firstChild;
template.model = [0, 1, 2];

then(function() {
assert.strictEqual(4, div.childNodes.length);
assert.strictEqual('0', div.childNodes[1].textContent);
assert.strictEqual('1', div.childNodes[2].textContent);
assert.strictEqual('2', div.childNodes[3].textContent);

// clear() synchronously removes instances
div.firstChild.clear();
assert.strictEqual(1, div.childNodes.length);

// test that template still works if new model assigned
template.model = [3, 4];

}).then(function() {
assert.strictEqual(3, div.childNodes.length);
assert.strictEqual('3', div.childNodes[1].textContent);
assert.strictEqual('4', div.childNodes[2].textContent);

done();
});
});

test('DOM Stability on Iteration', function(done) {
var div = createTestHtml(
'<template repeat="{{}}">{{}}</template>');
Expand Down

0 comments on commit cc00394

Please sign in to comment.