Skip to content

Commit b0b4925

Browse files
committed
Merge pull request #3537 from Polymer/also-delay-detached
Delay detached callback with the same strategy as attached callback
2 parents d316ff4 + 7a244fa commit b0b4925

File tree

3 files changed

+86
-5
lines changed

3 files changed

+86
-5
lines changed

src/lib/base.html

+9-5
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
},
5151

5252
/**
53-
* As an optimization, when `Polymer.Settings.lazyRegister` is set to true
54-
* registration tasks are deferred until the first instance of the element
53+
* As an optimization, when `Polymer.Settings.lazyRegister` is set to true
54+
* registration tasks are deferred until the first instance of the element
5555
* is created. If an element should not defer registration tasks until
56-
* this time, `ensureRegisterFinished` may be called
56+
* this time, `ensureRegisterFinished` may be called
5757
* on the element's prototype.
5858
*/
5959
ensureRegisterFinished: function() {
@@ -91,8 +91,12 @@
9191

9292
// reserved for canonical behavior
9393
detachedCallback: function() {
94-
this.isAttached = false;
95-
this._doBehavior('detached'); // abstract
94+
// NOTE: duplicate attachedCallback behavior
95+
var self = this;
96+
Polymer.RenderStatus.whenReady(function() {
97+
self.isAttached = false;
98+
self._doBehavior('detached'); // abstract
99+
});
96100
},
97101

98102
// reserved for canonical behavior

test/runner.html

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
var idx = suites.indexOf('unit/polymer-dom-shadow.html');
8080
suites.splice(idx, 0, 'unit/polymer-dom-native-shadow.html');
8181
}
82+
if (window.customElements || document.registerElement) {
83+
suites.push('unit/attach-detach-timing.html');
84+
}
8285
WCT.loadSuites(suites);
8386
</script>
8487
</body>

test/unit/attach-detach-timing.html

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<!DOCTYPE html>
2+
<!--
3+
@license
4+
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
5+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8+
Code distributed by Google as part of the polymer project is also
9+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10+
-->
11+
<!DOCTYPE html>
12+
<html>
13+
<head>
14+
<meta charset="utf-8">
15+
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
16+
<script>
17+
window.WCT = {
18+
// don't wait for HTML Imports to load
19+
waitFor: function(cb) {
20+
cb();
21+
}
22+
};
23+
</script>
24+
<script src="../../../web-component-tester/browser.js"></script>
25+
<link rel="import" href="../../polymer.html">
26+
<script>
27+
Polymer({
28+
is: 'attach-detach',
29+
properties: {
30+
stream: {
31+
type: Array,
32+
value: function(){ return []; }
33+
}
34+
},
35+
attached: function() {
36+
this.stream.push('attached');
37+
},
38+
detached: function() {
39+
this.stream.push('detached');
40+
}
41+
});
42+
</script>
43+
<script>
44+
function checkOrder(el) {
45+
assert.equal(el.stream.join(' '), [
46+
'attached',
47+
'detached'
48+
].join(' '));
49+
}
50+
</script>
51+
</head>
52+
<body>
53+
<attach-detach id="ad"></attach-detach>
54+
<script>
55+
var ad = document.getElementById('ad');
56+
ad.remove();
57+
var el = document.createElement('attach-detach');
58+
document.body.appendChild(el);
59+
el.remove();
60+
test('attach() and detach() are correct for upgraded elements', function() {
61+
checkOrder(ad);
62+
});
63+
test('attach() and detach() are correct for imperative elements', function() {
64+
checkOrder(el);
65+
});
66+
test('attach() and detach() are correct for elements made after load', function() {
67+
var e = document.createElement('attach-detach');
68+
document.body.appendChild(e);
69+
e.remove();
70+
checkOrder(e);
71+
});
72+
</script>
73+
</body>
74+
</html>

0 commit comments

Comments
 (0)