Skip to content

Commit 48446aa

Browse files
committed
Added select to array
1 parent 6886a42 commit 48446aa

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

example/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
<body>
1313
<js-container>
1414
<js-tag transform="uppercase">Data</js-tag>
15+
1516
<!-- where the data comes from -->
16-
<js-provider id="provider" origin="http://localhost:8080/" path="aktek/sync" interval="5"></js-provider>
17+
<js-provider id="provider" origin="http://localhost:8080/" path="aktek/area" interval="5"></js-provider>
1718

18-
<!-- model -->
19-
<js-array id="array" provider="#provider"></js-array>
19+
<!-- model, areas come from the body element -->
20+
<js-array id="array" provider="#provider" select="body"></js-array>
2021

2122
<!-- view -->
2223
<js-tablebody id="body" data="#array">

src/core/ArrayElement.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class ArrayElement extends LitElement {
2323
static get properties() {
2424
return {
2525
provider: { type: String, reflect: true },
26+
select: { type: String, reflect: true },
2627
};
2728
}
2829

@@ -77,8 +78,22 @@ export class ArrayElement extends LitElement {
7778
}
7879

7980
#providerObject(event) {
80-
// Add the object to the data container
81-
this.#newdata.push(event.detail);
81+
var data = event.detail;
82+
if (this.select) {
83+
if(data instanceof Object) {
84+
data = data[this.select];
85+
if (data === undefined) {
86+
throw new Error(`Property "${this.select}" not found in object`);
87+
} else if (Array.isArray(data)) {
88+
this.#newdata = this.#newdata.concat(data);
89+
} else {
90+
this.#newdata.push(data);
91+
}
92+
}
93+
} else {
94+
// Add the object to the data container
95+
this.#newdata.push(data);
96+
}
8297
}
8398

8499
#providerDone() {
@@ -96,7 +111,7 @@ export class ArrayElement extends LitElement {
96111

97112
// Copy over the data
98113
this.#data = this.#newdata;
99-
114+
100115
// Emit a change event if the data was modified
101116
if (modified) {
102117
this.dispatchEvent(new CustomEvent(EventType.CHANGE, {

0 commit comments

Comments
 (0)