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

Commit

Permalink
getInstanceModel -> prepareInstanceModel
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelw committed Sep 5, 2013
1 parent ad64879 commit 35f5c69
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
16 changes: 9 additions & 7 deletions src/TemplateBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
33 changes: 21 additions & 12 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
};

Expand All @@ -1961,22 +1964,28 @@ suite('Binding Delegate API', function() {
'<template repeat>' +
'{{}}</template>');
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() {
Expand Down

0 comments on commit 35f5c69

Please sign in to comment.