From dbbd7e7c5899179bd2fbf11a46ddeda207e9552f Mon Sep 17 00:00:00 2001 From: Rafael Weinstein Date: Tue, 24 Sep 2013 18:15:00 -0700 Subject: [PATCH] delegate must be correctly scoped https://github.com/Polymer/polymer/issues/285 R=arv Review URL: https://codereview.appspot.com/13883043 --- src/TemplateBinding.js | 3 ++- tests/tests.js | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/TemplateBinding.js b/src/TemplateBinding.js index f2851bf..8d32284 100644 --- a/src/TemplateBinding.js +++ b/src/TemplateBinding.js @@ -1006,13 +1006,14 @@ return; var template = this.templateElement_; + var delegate = template.bindingDelegate; + if (!template.parentNode || !template.ownerDocument.defaultView) { this.close(); return; } if (this.instanceModelFn_ === undefined) { - var delegate = template.bindingDelegate; if (delegate && typeof delegate.prepareInstanceModel === 'function') this.instanceModelFn_ = delegate.prepareInstanceModel(template); if (typeof this.instanceModelFn_ !== 'function') diff --git a/tests/tests.js b/tests/tests.js index b41e81c..6672c42 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1846,8 +1846,43 @@ suite('Template Instantiation', function() { assert.strictEqual(1, template3.content.childNodes.length); assert.strictEqual('Hello', template3.content.firstChild.textContent); }); -}); + test('issue-285', function() { + var div = createTestHtml( + ''); + + var template = div.firstChild; + + var model = { + show: true, + items: [1] + }; + + div.appendChild(template.createInstance(model, { + prepareInstanceModel: function(template) { + if (template.id == 'del') { + return function(val) { + return val*2; + }; + } + } + })); + + Platform.performMicrotaskCheckpoint(); + assert.equal('2', template.nextSibling.nextSibling.nextSibling.textContent); + model.show = false; + Platform.performMicrotaskCheckpoint(); + model.show = true; + Platform.performMicrotaskCheckpoint(); + assert.equal('2', template.nextSibling.nextSibling.nextSibling.textContent); + }); +}); suite('Binding Delegate API', function() {