Skip to content
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

Introducing an insertBefore method on Templatize instances #4492

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion lib/elements/dom-if.html
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
}
if (!this.__instance) {
this.__instance = new this.__ctor();
parentNode.insertBefore(this.__instance.root, this);
this.__instance.connect(parentNode, this);
} else {
this.__syncHostProperties();
let c$ = this.__instance.children;
Expand Down
2 changes: 1 addition & 1 deletion lib/elements/dom-repeat.html
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@
}
let beforeRow = this.__instances[instIdx + 1];
let beforeNode = beforeRow ? beforeRow.children[0] : this;
this.parentNode.insertBefore(inst.root, beforeNode);
inst.connect(this.parentNode, beforeNode);
this.__instances[instIdx] = inst;
return inst;
}
Expand Down
40 changes: 34 additions & 6 deletions lib/utils/templatize.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@
if (this.__templatizeOwner.__hideTemplateChildren__) {
this._showHideChildren(true);
}
// Flush props only when props are passed if instance props exist
// or when there isn't instance props.
let options = this.__templatizeOptions;
if ((props && options.instanceProps) || !options.instanceProps) {
this._flushProperties();
}
}
/**
* @private
Expand All @@ -77,6 +71,40 @@
this._setPendingProperty(hprop, this.__dataHost['_host_' + hprop]);
}
}
/**
Inserts the templatized dom into the given `parentNode` before
`refNode`.
@param {Node} parentNode node into which to insert dom
@param {Node} refNode node before which to insert dom
*/
connect(parentNode, refNode) {
if (!this.__dataClientsInitialized) {
this.__parentNode = parentNode;
this.__refNode = refNode;
this._flushProperties();
} else {
for (let i=0, l=this.children.length; i < l; i++) {
parentNode.insertBefore(this.children[i], refNode);
}
}
}

/**
Removes the templatized dom.
*/
disconnect() {
for (let i=0, l=this.children.length; i < l; i++) {
let c = this.children[i];
c.parentNode.removeChild(c);
}
}

_readyClients() {
super._readyClients();
this.__parentNode.insertBefore(this.root, this.__refNode);
// no longer needed, don't hold reference
this.__parentNode = this.__refNode = null;
}
/**
* Forwards a host property to this instance. This method should be
* called on instances from the `options.forwardHostProp` callback
Expand Down
72 changes: 72 additions & 0 deletions test/smoke/ready-order.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!--
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<!DOCTYPE html>
<html>
<head>
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../../polymer.html">
</head>
<body>
<dom-module id="grandchild-element">
<script>
Polymer({
is: 'grandchild-element',
ready: function(){
console.log('grandchild-element ready');
}
});
</script>
</dom-module>
<dom-module id="child-element">
<template>
<grandchild-element></grandchild-element>
<h2>{{person}}</h2>
</template>
<script>
Polymer({
is: 'child-element',
properties: {
person: {
type: Object
}
},
ready: function(){
console.log('child-element ready');
}
});
</script>
</dom-module>
<dom-module id="parent-element">
<template>
<template is="dom-repeat" items="[[childrenData]]">
<child-element person="[[item]]"></child-element>
</template>
</template>
<script>
Polymer({
is: 'parent-element',
properties: {
childrenData: {
type: Array,
value: function() { return ["Child name"]}
}
},
ready: function(){
console.log('parent-element ready');
}
});
</script>
</dom-module>


<parent-element></parent-element>

</body>
</html>
4 changes: 2 additions & 2 deletions test/unit/templatize-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
prop: 'bar'
}
} : null);
this.parentNode.appendChild(this.instance.root);
this.instance.connect(this.parentNode)
}
});

Expand Down Expand Up @@ -190,7 +190,7 @@
prop: 'bar'
}
} : null);
this.parentNode.appendChild(this.instance.root);
this.instance.connect(this.parentNode);
}
});

Expand Down