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

Commit

Permalink
make ref bindable
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelw committed Feb 6, 2014
1 parent ef5535a commit 2799341
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/TemplateBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,15 @@
}
}

var templateObserver;
if (typeof MutationObserver == 'function') {
templateObserver = new MutationObserver(function(records) {
for (var i = 0; i < records.length; i++) {
records[i].target.refChanged_();
}
});
}

/**
* Ensures proper API and content model for template elements.
* @param {HTMLTemplateElement} opt_instanceRef The template element which
Expand Down Expand Up @@ -429,6 +438,25 @@
}

mixin(HTMLTemplateElement.prototype, {
bind: function(name, value, oneTime) {
if (name != 'ref')
return Element.prototype.bind.call(this, name, value, oneTime);

var self = this;
var ref = oneTime ? value : value.open(function(ref) {
self.setAttribute('ref', ref);
self.refChanged_();
});

this.setAttribute('ref', ref);
this.refChanged_();
if (oneTime)
return;

this.unbind('ref');
return this.bindings.ref = value;
},

processBindingDirectives_: function(directives) {
if (this.iterator_)
this.iterator_.closeDeps();
Expand All @@ -450,6 +478,12 @@
}

this.iterator_.updateDependencies(directives, this.model_);

if (templateObserver) {
templateObserver.observe(this, { attributes: true,
attributeFilter: ['ref'] });
}

return this.iterator_;
},

Expand All @@ -458,7 +492,9 @@
if (bindingDelegate)
delegate_ = this.newDelegate_(bindingDelegate);

var content = this.ref_.content;
if (!this.refContent_)
this.refContent_ = this.ref_.content;
var content = this.refContent_;
var map = this.bindingMap_;
if (!map || map.content !== content) {
// TODO(rafaelw): Setup a MutationObserver on content to detect
Expand Down Expand Up @@ -510,10 +546,20 @@
return this.delegate_ && this.delegate_.raw;
},

refChanged_: function() {
if (!this.iterator_ || this.refContent_ === this.ref_.content)
return;

this.refContent_ = undefined;
this.iterator_.valueChanged();
this.iterator_.updateIteratedValue();
},

clear: function() {
this.model_ = undefined;
this.delegate_ = undefined;
this.bindings_ = undefined;
this.refContent_ = undefined;
if (!this.iterator_)
return;
this.iterator_.valueChanged();
Expand Down
31 changes: 31 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,12 @@ suite('Template Instantiation', function() {
});

test('Update Ref', function(done) {
// Updating ref by observing the attribute is dependent on MutationObservers
if (typeof MutationObserver != 'function') {
done();
return;
}

var div = createTestHtml(
'<template id=A>Hi, {{}}</template>' +
'<template id=B>Hola, {{}}</template>' +
Expand All @@ -1516,7 +1522,32 @@ suite('Template Instantiation', function() {

}).then(function() {
assert.strictEqual(5, div.childNodes.length);
assert.strictEqual('Hola, Fry', div.childNodes[3].textContent);
assert.strictEqual('Hola, Leela', div.childNodes[4].textContent);

done();
});
});

test('Bound Ref', function(done) {
var div = createTestHtml(
'<template id=A>Hi, {{}}</template>' +
'<template id=B>Hola, {{}}</template>' +
'<template ref="{{ ref }}" repeat="{{ people }}"></template>');
var template = div.childNodes[2];
var model = { ref: 'A', people: ['Fry'] };
template.model = model;

then(function() {
assert.strictEqual(4, div.childNodes.length);
assert.strictEqual('Hi, Fry', div.childNodes[3].textContent);

model.ref = 'B';
model.people.push('Leela');

}).then(function() {
assert.strictEqual(5, div.childNodes.length);
assert.strictEqual('Hola, Fry', div.childNodes[3].textContent);
assert.strictEqual('Hola, Leela', div.childNodes[4].textContent);

done();
Expand Down

0 comments on commit 2799341

Please sign in to comment.