Skip to content

Allow templatizer to be used without owner or host prop forwarding. Fixes #4458 #5035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions lib/utils/templatize.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
children.push(n);
n.__templatizeInstance = this;
}
if (this.__templatizeOwner.__hideTemplateChildren__) {
if (this.__templatizeOwner &&
this.__templatizeOwner.__hideTemplateChildren__) {
this._showHideChildren(true);
}
// Flush props only when props are passed if instance props exist
Expand All @@ -96,15 +97,13 @@
*/
_configureProperties(props) {
let options = this.__templatizeOptions;
if (props) {
for (let iprop in options.instanceProps) {
if (iprop in props) {
this._setPendingProperty(iprop, props[iprop]);
}
if (options.forwardHostProp) {
for (let hprop in this.__hostProps) {
this._setPendingProperty(hprop, this.__dataHost['_host_' + hprop]);
}
}
for (let hprop in this.__hostProps) {
this._setPendingProperty(hprop, this.__dataHost['_host_' + hprop]);
for (let iprop in props) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment saying that passing in a prop in props that has the same name as a bound host prop may shadow it. This is considered a user error (warn?). We should add this to the docs too.

this._setPendingProperty(iprop, props[iprop]);
}
}
/**
Expand Down Expand Up @@ -226,6 +225,15 @@
}
return model;
}

/**
* Stub of HTMLElement's `dispatchEvent`, so that effects that may
* dispatch events safely no-op.
*
* @param {Event} event Event to dispatch
*/
dispatchEvent(event) { // eslint-disable-line no-unused-vars
}
}

/** @type {!DataTemplate} */
Expand Down Expand Up @@ -464,7 +472,8 @@
throw new Error('A <template> can only be templatized once');
}
template.__templatizeOwner = owner;
let templateInfo = owner.constructor._parseTemplate(template);
const ctor = owner ? owner.constructor : TemplateInstanceBase;
let templateInfo = ctor._parseTemplate(template);
// Get memoized base class for the prototypical template, which
// includes property effects for binding template & forwarding
let baseClass = templateInfo.templatizeInstanceClass;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"polymer-build": "^2.1.1",
"run-sequence": "^2.2.0",
"through2": "^2.0.0",
"web-component-tester": "^6.4.1"
"web-component-tester": "^6.4.2"
},
"scripts": {
"build": "gulp",
Expand Down
40 changes: 40 additions & 0 deletions test/unit/templatize.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
</head>
<body>

<template id="standalone">
<div prop="[[prop]]">[[text]]</div>
</template>

<script>

suite('templatize basic', function() {
Expand Down Expand Up @@ -328,6 +332,42 @@

});

suite('templatize with no host', function() {

test('stamped with initial data', function() {
const template = document.getElementById('standalone').cloneNode(true);
const Template = Polymer.Templatize.templatize(template);
const inst = new Template({prop: 'prop', text: 'text'});
const div = inst.root.firstElementChild;
assert.equal(div.prop, 'prop');
assert.equal(div.textContent, 'text');
});

test('stamped with no initial data', function() {
const template = document.getElementById('standalone').cloneNode(true);
const Template = Polymer.Templatize.templatize(template);
const inst = new Template();
const div = inst.root.firstElementChild;
assert.equal(div.prop, undefined);
assert.equal(div.textContent.trim(), '');
inst.setProperties({prop: 'prop', text: 'text'});
assert.equal(div.prop, 'prop');
assert.equal(div.textContent, 'text');
});

test('notifies path data changes', function() {
const template = document.getElementById('standalone').cloneNode(true);
const Template = Polymer.Templatize.templatize(template);
const inst = new Template();
const div = inst.root.firstElementChild;
inst.setProperties({prop: {foo: true}});
assert.equal(div.prop.foo, true);
inst.set('prop.foo', false);
assert.equal(div.prop.foo, false);
});

});

</script>
</body>
</html>
8 changes: 8 additions & 0 deletions types/lib/utils/templatize.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ declare class TemplateInstanceBase extends
* set to false to show them.
*/
_showHideChildren(hide: boolean): void;

/**
* Stub of HTMLElement's `dispatchEvent`, so that effects that may
* dispatch events safely no-op.
*
* @param event Event to dispatch
*/
dispatchEvent(event: Event|null): any;
}

declare namespace templateInfo {
Expand Down