-
Notifications
You must be signed in to change notification settings - Fork 0
/
polymer-list-item-notify-bridge.html
68 lines (62 loc) · 1.94 KB
/
polymer-list-item-notify-bridge.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<link rel="import" href="../polymer/polymer.html">
<!--
`polymer-list-item-notify-bridge`
A polymer element that does the appropriate notifyPath() calls when editing a specific model of a list
@demo demo/index.html
-->
<dom-module id="polymer-list-item-notify-bridge">
<template></template>
<script>
Polymer({
is: 'polymer-list-item-notify-bridge',
properties: {
items: {
type: Array,
default: function (f) {
return [];
},
notify: true
},
selectedItem: {
type: Object,
default: function (f) {
return {};
},
notify: true
}
},
observers: [
'_itemsChanged(items.*)',
'_selectedItemChanged(selectedItem.*)'
],
_itemsChanged: function (changeRecord) {
if (!this.items || !this.items.length) {
return;
}
var i = this.items.indexOf(this.selectedItem);
if (i < 0) {
return;
}
var changedIndexStr = (changeRecord.path.match(/^items\.#(\d+)\./) || [])[1];
if (!changedIndexStr || i !== Number(changedIndexStr)) {
return;
}
this.notifyPath(
changeRecord.path.replace(/^items\.#(\d+)\./, 'selectedItem.'),
this.get(changeRecord.path));
},
_selectedItemChanged: function(changeRecord) {
if (!this.items || !this.items.length) {
return;
}
var i = this.items.indexOf(this.selectedItem);
if (i < 0) {
return;
}
this.notifyPath(
changeRecord.path.replace('selectedItem', 'items.#' + i),
this.get(changeRecord.path));
}
});
</script>
</dom-module>