File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
src/client/reactive/polymorphic-identifiers Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments