Permalink
Browse files

Implement Syntax getInstanceModel delegate function

  • Loading branch information...
rafaelw committed May 7, 2013
1 parent ca6079d commit b6af70e922c49fc6fc85e782e549831d3680712e
Showing with 59 additions and 3 deletions.
  1. +10 −3 src/template_element.js
  2. +49 −0 tests/syntax.js
View
@@ -412,6 +412,7 @@
var IF = 'if';
var SYNTAX = 'syntax';
var GET_BINDING = 'getBinding';
+ var GET_INSTANCE_MODEL = 'getInstanceModel';
var templateAttributeDirectives = {
'template': true,
@@ -1123,8 +1124,13 @@
this.iteratedValue = undefined;
},
- getInstanceModel: function(model, syntax) {
- return model;
+ getInstanceModel: function(template, model, syntax) {
+ var delegateModel;
+ var delegate = syntax && syntax[GET_INSTANCE_MODEL];
+ if (delegate && typeof delegate == 'function')
+ return delegate(template, model);
+ else
+ return model;
},
getInstanceFragment: function(syntax) {
@@ -1142,7 +1148,8 @@
var addIndex = splice.index;
for (; addIndex < splice.index + splice.addedCount; addIndex++) {
- var model = this.getInstanceModel(this.iteratedValue[addIndex],
+ var model = this.getInstanceModel(this.templateElement_,
+ this.iteratedValue[addIndex],
syntax);
var fragment = this.getInstanceFragment(syntax);
View
@@ -100,6 +100,55 @@ suite('Syntax', function() {
delete HTMLTemplateElement.syntax['Test'];
});
+ test('getInstanceModel', function() {
+ var model = [{ foo: 1 }, { foo: 2 }, { foo: 3 }];
+
+ var div = createTestHtml(
+ '<template repeat syntax="Test">' +
+ '{{ foo }}</template>');
+ var template = div.firstChild;
+
+ 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' }
+ }
+ ];
+
+ HTMLTemplateElement.syntax['Test'] = {
+ getInstanceModel: function(template, model) {
+ var data = testData.shift();
+
+ assert.strictEqual(data.template, template);
+ assert.strictEqual(data.model, model);
+ return data.altModel;
+ }
+ };
+
+ recursivelySetTemplateModel(div, model);
+ Platform.performMicrotaskCheckpoint();
+ assert.strictEqual(4, div.childNodes.length);
+ assert.strictEqual('TEMPLATE', div.childNodes[0].tagName);
+ assert.strictEqual('a', div.childNodes[1].textContent);
+ assert.strictEqual('b', div.childNodes[2].textContent);
+ assert.strictEqual('c', div.childNodes[3].textContent);
+
+ assert.strictEqual(0, testData.length);
+
+ delete HTMLTemplateElement.syntax['Test'];
+ });
+
test('Basic', function() {
var model = { foo: 2, bar: 4 };

0 comments on commit b6af70e

Please sign in to comment.