rasti 1.1.3 → 2.0.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/src/View.js CHANGED
@@ -16,13 +16,15 @@ const viewOptions = {
16
16
  * - Handles user input and interactivity.
17
17
  * - Sends captured input to the model.
18
18
  *
19
- * A View is an atomic chunk of user interface. It often renders the data from a specific model,
20
- * or number of models, but views can also be data-less chunks of UI that stand alone.<br />
21
- * Models must be unaware of views. Instead, views listen to the model "change" events,
22
- * and react or re-render themselves appropriately.<br />
23
- * Views has a root element, `this.el`. That element is used for event delegation. Elements lookups are scoped to that element. And render and dom manipulations should be done inside that element.
24
- * If `this.el` is not present, an element will be created using `this.tag` (or `div` as default), and `this.attributes`.<br />
19
+ * A `View` is an atomic unit of the user interface that can render the data from a specific model or multiple models.
20
+ * However, views can also be independent and have no associated data.<br />
21
+ * Models must be unaware of views. Views, on the other hand, may render model data and listen to the change events
22
+ * emitted by the models to re-render themselves based on changes.<br />
23
+ * Each `View` has a root element, `this.el`, which is used for event delegation.<br />
24
+ * All element lookups are scoped to this element, and any rendering or DOM manipulations should be done inside it.
25
+ * If `this.el` is not present, an element will be created using `this.tag` (defaulting to div) and `this.attributes`.
25
26
  * @module
27
+ * @extends Rasti.Emitter
26
28
  * @param {object} options Object containing options. The following keys will be merged to `this`: el, tag, attributes, events, model, template, onDestroy.
27
29
  * @property {node} el Every view has a root element, `this.el`. If not present it will be created.
28
30
  * @property {string} tag If `this.el` is not present, an element will be created using `this.tag`. Default is `div`.
@@ -31,43 +33,25 @@ const viewOptions = {
31
33
  * @property {object} model A `Rasti.Model` or any object containing data and business logic.
32
34
  * @property {function} template A function that receives data and returns a markup string (html for example).
33
35
  * @example
34
- * // Counter view.
35
- * class CounterView extends View {
36
+ * import { View } from 'rasti';
37
+ *
38
+ * class Timer extends View {
36
39
  * constructor(options) {
37
40
  * super(options);
38
- * // Bind method to `this`, to be called as listener.
39
- * this.render = this.render.bind(this);
40
- * // Listen to model change and re render.
41
- * this.model.on('change:count', this.render);
41
+ * // Create model to store internal state. Set `seconds` attribute into 0.
42
+ * this.model = new Model({ seconds : 0 });
43
+ * // Listen to changes in model `seconds` attribute and re render.
44
+ * this.model.on('change:seconds', this.render.bind(this));
45
+ * // Increment model `seconds` attribute every 1000 milliseconds.
46
+ * this.interval = setInterval(() => this.model.seconds++, 1000);
42
47
  * }
43
- * onDestroy() {
44
- * // Unbind events when destroyed.
45
- * this.model.off('change:count', this.render);
46
- * }
47
- * // Listener method. Called when button is clicked.
48
- * onClickIncrement() {
49
- * // Increment count on model.
50
- * this.model.count++;
48
+ *
49
+ * template(model) {
50
+ * return `Seconds: <span>${model.seconds}</span>`;
51
51
  * }
52
52
  * }
53
- * Object.assign(CounterView.prototype, {
54
- * // Set delegated events.
55
- * // Call `onClickIncrement` when button is clicked.
56
- * events : {
57
- * 'click button' : 'onClickIncrement'
58
- * },
59
- * // View's template.
60
- * template : (model) => `
61
- * <div>The count is: ${model.count}</div>
62
- * <button>Increment</button>
63
- * `
64
- * });
65
- * // Model.
66
- * const model = new Model({ count : 0 });
67
- * // Instantiate CounterView.
68
- * const counterView = new CounterView({ model });
69
- * // Add to DOM.
70
- * document.body.appendChild(counterView.render().el);
53
+ * // Render view and append view's element into body.
54
+ * document.body.appendChild(new Timer().render().el);
71
55
  */
72
56
  export default class View extends Emitter {
73
57
  constructor(options = {}) {
@@ -133,7 +117,7 @@ export default class View extends Emitter {
133
117
  // Remove `this.el` if "options.remove" is true.
134
118
  if (remove) this.removeElement();
135
119
  // Call onDestroy lifecycle method
136
- this.onDestroy();
120
+ this.onDestroy.apply(this, arguments);
137
121
  }
138
122
 
139
123
  /**
package/src/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import Emitter from './Emitter.js';
2
2
  import Model from './Model.js';
3
3
  import View from './View.js';
4
+ import Component from './Component.js';
4
5
 
5
- export { Emitter, Model, View };
6
+ export { Emitter, Model, View, Component };