@symbiotejs/symbiote 2.0.5 → 2.1.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/core/Symbiote.js CHANGED
@@ -162,6 +162,8 @@ export class Symbiote extends HTMLElement {
162
162
  this.ctxOwner = false;
163
163
  /** @type {Boolean} */
164
164
  this.isVirtual = false;
165
+ /** @type {Boolean} */
166
+ this.allowTemplateInits = true;
165
167
  }
166
168
 
167
169
  /** @returns {String} */
@@ -341,6 +343,24 @@ export class Symbiote extends HTMLElement {
341
343
  return this.ctxOwner || (this.hasAttribute(DICT.CTX_OWNER_ATTR) && this.getAttribute(DICT.CTX_OWNER_ATTR) !== 'false');
342
344
  }
343
345
 
346
+ initAttributeObserver() {
347
+ if (!this.attributeMutationObserver) {
348
+ this.attributeMutationObserver = new MutationObserver((mutations) => {
349
+ for (let mr of mutations) {
350
+ if (mr.type === 'attributes') {
351
+ let propName = DICT.ATTR_BIND_PX + mr.attributeName;
352
+ if (this.has(propName)) {
353
+ this.$[propName] = this.getAttribute(mr.attributeName);
354
+ }
355
+ }
356
+ }
357
+ });
358
+ this.attributeMutationObserver.observe(this, {
359
+ attributes: true,
360
+ });
361
+ }
362
+ }
363
+
344
364
  #initDataCtx() {
345
365
  /** @type {{ [key: string]: string }} */
346
366
  let attrDesc = this.#super.__attrDesc;
@@ -354,6 +374,9 @@ export class Symbiote extends HTMLElement {
354
374
  for (let prop in this.init$) {
355
375
  if (prop.startsWith(DICT.SHARED_CTX_PX)) {
356
376
  this.sharedCtx.add(prop.replace(DICT.SHARED_CTX_PX, ''), this.init$[prop], this.#ctxOwner);
377
+ } else if (prop.startsWith(DICT.ATTR_BIND_PX)) {
378
+ this.localCtx.add(prop, (this.getAttribute(prop.replace(DICT.ATTR_BIND_PX, '')) || this.init$[prop]));
379
+ this.initAttributeObserver();
357
380
  } else if (prop.includes(DICT.NAMED_CTX_SPLTR)) {
358
381
  let propArr = prop.split(DICT.NAMED_CTX_SPLTR);
359
382
  let ctxName = propArr[0].trim();
@@ -443,6 +466,9 @@ export class Symbiote extends HTMLElement {
443
466
  }
444
467
  this.#disconnectTimeout = window.setTimeout(() => {
445
468
  this.destroyCallback();
469
+ if (this.attributeMutationObserver) {
470
+ this.attributeMutationObserver.disconnect();
471
+ }
446
472
  for (let sub of this.allSubs) {
447
473
  sub.remove();
448
474
  this.allSubs.delete(sub);
@@ -491,7 +517,7 @@ export class Symbiote extends HTMLElement {
491
517
 
492
518
  /** @param {Object<string, string>} desc */
493
519
  static bindAttributes(desc) {
494
- this.observedAttributes = Object.keys(desc);
520
+ this.observedAttributes = [...(this.observedAttributes || []), Object.keys(desc)];
495
521
  /** @private */
496
522
  this.__attrDesc = desc;
497
523
  }
@@ -52,6 +52,13 @@ function domBindProcessor(fr, fnCtx) {
52
52
  castType = 'single';
53
53
  valKey = valKey.replace('!', '');
54
54
  }
55
+ if (!fnCtx.has(valKey) && fnCtx.allowTemplateInits) {
56
+ if (valKey.startsWith(DICT.ATTR_BIND_PX)) {
57
+ fnCtx.add(valKey, fnCtx.getAttribute(valKey.replace(DICT.ATTR_BIND_PX, '')));
58
+ } else {
59
+ fnCtx.add(valKey, null);
60
+ }
61
+ }
55
62
  fnCtx.sub(valKey, (val) => {
56
63
  if (castType === 'double') {
57
64
  val = !!val;
@@ -123,6 +130,14 @@ const txtNodesProcessor = function (fr, fnCtx) {
123
130
  tokenNodes.forEach((tNode) => {
124
131
  let prop = tNode.textContent.replace(DICT.TEXT_NODE_OPEN_TOKEN, '').replace(DICT.TEXT_NODE_CLOSE_TOKEN, '');
125
132
  tNode.textContent = '';
133
+ if (!fnCtx.has(prop) && fnCtx.allowTemplateInits) {
134
+ if (prop.startsWith(DICT.ATTR_BIND_PX)) {
135
+ fnCtx.add(prop, fnCtx.getAttribute(prop.replace(DICT.ATTR_BIND_PX, '')));
136
+ fnCtx.initAttributeObserver();
137
+ } else {
138
+ fnCtx.add(prop, null);
139
+ }
140
+ }
126
141
  fnCtx.sub(prop, (val) => {
127
142
  tNode.textContent = val;
128
143
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@symbiotejs/symbiote",
4
- "version": "2.0.5",
4
+ "version": "2.1.0-alpha.1",
5
5
  "scripts": {
6
6
  "types": "rm -rf types && tsc -p dts.cfg.json"
7
7
  },