diff --git a/src/TemplateBinding.js b/src/TemplateBinding.js index 62b66f2..d452ba7 100644 --- a/src/TemplateBinding.js +++ b/src/TemplateBinding.js @@ -1012,11 +1012,13 @@ return; } - var delegate = template.bindingDelegate; - var delegateInstanceModelFn = - delegate && typeof delegate.getInstanceModel === 'function' ? - delegate.getInstanceModel : undefined; - + if (this.instanceModelFn_ === undefined) { + var delegate = template.bindingDelegate; + if (delegate && typeof delegate.prepareInstanceModel === 'function') + this.instanceModelFn_ = delegate.prepareInstanceModel(template); + if (typeof this.instanceModelFn_ !== 'function') + this.instanceModelFn_ = false; + } var instanceCache = new Map; var removeDelta = 0; @@ -1042,8 +1044,8 @@ bound = instanceNodes.bound; } else { bound = []; - if (delegateInstanceModelFn) - model = delegateInstanceModelFn(template, model); + if (this.instanceModelFn_) + model = this.instanceModelFn_(model); if (model !== undefined) { fragment = this.templateElement_.createInstance(model, diff --git a/tests/tests.js b/tests/tests.js index a103bff..8672058 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1918,28 +1918,31 @@ suite('Binding Delegate API', function() { var testData = [ { template: template, + }, + { model: model[0], altModel: { foo: 'a' } }, { - template: template, model: model[1], altModel: { foo: 'b' } }, { - template: template, model: model[2], altModel: { foo: 'c' } } ]; var delegate = { - getInstanceModel: function(template, model) { + prepareInstanceModel: function(template) { var data = testData.shift(); - assert.strictEqual(data.template, template); - assert.strictEqual(data.model, model); - return data.altModel; + + return function(model) { + data = testData.shift(); + assert.strictEqual(data.model, model); + return data.altModel; + } } }; @@ -1961,22 +1964,28 @@ suite('Binding Delegate API', function() { ''); var template = div.firstChild; - var count = 0; + var prepareCount = 0; + var callCount = 0; var delegate = { - getInstanceModel: function(template, model) { - count++; - return model; + prepareInstanceModel: function(template) { + prepareCount++; + return function(model) { + callCount++; + return model; + }; } }; recursivelySetTemplateModel(div, model, delegate); Platform.performMicrotaskCheckpoint(); - assert.strictEqual(3, count); + assert.strictEqual(1, prepareCount); + assert.strictEqual(3, callCount); model.reverse(); Platform.performMicrotaskCheckpoint(); - assert.strictEqual(3, count); + assert.strictEqual(1, prepareCount); + assert.strictEqual(3, callCount); }); test('Basic', function() {