rasti 3.0.0-alpha.3 → 3.0.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/lib/View.cjs CHANGED
@@ -6,15 +6,7 @@ var utils_getResult = require('./utils/getResult.cjs');
6
6
  /*
7
7
  * These option keys will be extended on the view instance.
8
8
  */
9
- const viewOptions = {
10
- el : true,
11
- tag : true,
12
- attributes : true,
13
- events : true,
14
- model : true,
15
- template : true,
16
- onDestroy : true
17
- };
9
+ const viewOptions = ['el', 'tag', 'attributes', 'events', 'model', 'template', 'onDestroy'];
18
10
 
19
11
  /**
20
12
  * - Listens for changes and renders the UI.
@@ -37,6 +29,7 @@ const viewOptions = {
37
29
  * @property {object|function} events Object in the format `{'event selector' : 'listener'}`. It will be used to bind delegated event listeners to the root element. If it is a function, it will be called to get the events object, bound to the view instance. See {@link module_view_delegateevents View.delegateEvents}.
38
30
  * @property {object} model A model or any object containing data and business logic.
39
31
  * @property {function} template A function that returns a string with the view's inner HTML. See {@link module_view__render View.render}.
32
+ * @property {number} uid Unique identifier for the view instance. This can be used to generate unique IDs for elements within the view. It is automatically generated and should not be set manually.
40
33
  * @example
41
34
  * import { View } from 'rasti';
42
35
  *
@@ -65,7 +58,7 @@ class View extends Emitter {
65
58
  this.preinitialize.apply(this, arguments);
66
59
  // Generate unique id.
67
60
  // Useful to generate element ids.
68
- this.uid = `uid${++View.uid}`;
61
+ this.uid = `rasti-${++View.uid}`;
69
62
  // Store delegated event listeners,
70
63
  // so they can be unbound later.
71
64
  this.delegatedEventListeners = [];
@@ -74,9 +67,9 @@ class View extends Emitter {
74
67
  this.children = [];
75
68
  // Mutable array to store handlers to be called on destroy.
76
69
  this.destroyQueue = [];
77
- // Extend "this" with options, mapping viewOptions keys.
78
- Object.keys(options).forEach(key => {
79
- if (viewOptions[key]) this[key] = options[key];
70
+ // Extend "this" with options.
71
+ viewOptions.forEach(key => {
72
+ if (key in options) this[key] = options[key];
80
73
  });
81
74
  // Ensure that the view has a root element at `this.el`.
82
75
  this.ensureElement();
@@ -305,17 +298,16 @@ class View extends Emitter {
305
298
  }
306
299
 
307
300
  /**
308
- * Renders the view.
301
+ * Renders the view.
309
302
  * This method should be overridden with custom logic.
310
303
  * The only convention is to manipulate the DOM within the scope of `this.el`,
311
- * and to return `this` for chaining.
312
- * If you add any child views, you should call `this.destroyChildren` before re-rendering.
313
- * The default implementation sets the innerHTML of `this.el` with the result
314
- * of calling `this.template`, passing `this.model` as an argument.
315
- * <br><br> &#9888; **Security Notice:** The default implementation utilizes `innerHTML` on the root element
316
- * for rendering, which may introduce Cross-Site Scripting (XSS) risks. Ensure that any user-generated
317
- * content is properly sanitized before inserting it into the DOM. You can use the @link{#module_view_sanitize View.sanitize}
318
- * static method to escape HTML entities in a string.
304
+ * and to return `this` for chaining.
305
+ * If you add any child views, you should call `this.destroyChildren` before re-rendering.
306
+ * The default implementation updates `this.el`'s innerHTML with the result
307
+ * of calling `this.template`, passing `this.model` as the argument.
308
+ * <br><br> &#9888; **Security Notice:** The default implementation utilizes `innerHTML`, which may introduce Cross-Site Scripting (XSS) risks.
309
+ * Ensure that any user-generated content is properly sanitized before inserting it into the DOM.
310
+ * You can use the {@link #module_view_sanitize View.sanitize} static method to escape HTML entities in a string.
319
311
  * For best practices on secure data handling, refer to the
320
312
  * [OWASP's XSS Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html).<br><br>
321
313
  * @return {Rasti.View} Returns `this` for chaining.
@@ -328,9 +320,9 @@ class View extends Emitter {
328
320
 
329
321
  /**
330
322
  * Escape HTML entities in a string.
331
- * Use method to sanitize user-generated content before inserting it into the DOM.
323
+ * Use this method to sanitize user-generated content before inserting it into the DOM.
332
324
  * Override this method to provide a custom escape function.
333
- * This method is used by `Component` to escape template interpolations.
325
+ * This method is inherited by {@link #module_component Component} and used to escape template interpolations.
334
326
  * @static
335
327
  * @param {string} str String to escape.
336
328
  * @return {string} Escaped string.
@@ -346,8 +338,16 @@ class View extends Emitter {
346
338
  }
347
339
  }
348
340
 
349
- /*
350
- * Unique Id
341
+ /**
342
+ * Counter for generating unique IDs for view instances.
343
+ * This is primarily used to assign unique identifiers to each view instance (`this.uid`), which can be helpful for tasks like
344
+ * generating element IDs.
345
+ * {@link #module_component Component}s use `this.uid` to generate data attributes for their elements, to be looked up on hydration.
346
+ * For server-side rendering, this counter should be reset to `0` on every request to ensure that the generated
347
+ * unique IDs match those on the client, enabling seamless hydration of components.
348
+ * @static
349
+ * @type {number}
350
+ * @default 0
351
351
  */
352
352
  View.uid = 0;
353
353
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rasti",
3
- "version": "3.0.0-alpha.3",
3
+ "version": "3.0.1",
4
4
  "description": "Modern MVC for building user interfaces",
5
5
  "type": "module",
6
6
  "main": "lib/index.cjs",
@@ -30,7 +30,7 @@
30
30
  "posttest": "npm run lint",
31
31
  "lint": "eslint src test",
32
32
  "test": "mocha --require jsdom-global/register --reporter nyan test/*.js",
33
- "docs:api": "jsdoc2md --property-list-format list --helper jsdoc2md/helper.cjs --partial jsdoc2md/header.hbs --partial jsdoc2md/sig-link.hbs --partial jsdoc2md/sig-link-html.hbs --partial jsdoc2md/sig-link-parent.hbs --partial jsdoc2md/link.hbs --files 'src/**/*.js' > docs/api.md"
33
+ "docs:api": "jsdoc2md --module-index-format grouped --helper jsdoc2md/helper.cjs --partial jsdoc2md/header.hbs --partial jsdoc2md/sig-link.hbs --partial jsdoc2md/sig-link-html.hbs --partial jsdoc2md/sig-link-parent.hbs --partial jsdoc2md/link.hbs --files 'src/**/*.js' > docs/api.md"
34
34
  },
35
35
  "repository": {
36
36
  "type": "git",