Skip to content

Commit dae57df

Browse files
committed
example for pi
SQUASHED: AUTO-COMMIT-src-client-reactive-polymorphic-identifiers-index.md,
1 parent f93d4b7 commit dae57df

File tree

1 file changed

+40
-0
lines changed
  • src/client/reactive/polymorphic-identifiers

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Polymorphic-Identifiers
2+
3+
```JavaScript
4+
import { PIReference } from 'polymorphic-identifiers';
5+
6+
// example scheme
7+
class qa extends PIReference {
8+
read() {
9+
const { elements } = this.parse();
10+
return elements;
11+
}
12+
write(v) {
13+
const { elements, type, prop } = this.parse();
14+
15+
if (type === 'style') {
16+
return elements.forEach(e => e.style[prop] = v)
17+
}
18+
elements.forEach(e => e.innerHTML = v)
19+
}
20+
21+
// helper
22+
parse() {
23+
const [selector, type, prop] = this.strings.first.split('/');
24+
const elements = this.query(selector);
25+
return { selector, type, prop, elements };
26+
}
27+
query(selector) {
28+
return Array.from(document.querySelectorAll(selector));
29+
}
30+
}
31+
32+
// usage
33+
// calls `read`
34+
qa`lively-window`.forEach(lw => lively.showElement(lw))
35+
36+
// assignments with <<
37+
// calls `write`
38+
qa`#rect` << 'html'
39+
qa`#rect/style/border-color` << 'green'
40+
```

0 commit comments

Comments
 (0)