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/README.md +24 -28
- package/dist/rasti.js +14 -4
- package/dist/rasti.min.js +1 -1
- package/es/Component.js +445 -0
- package/es/Emitter.js +29 -11
- package/es/Model.js +35 -26
- package/es/View.js +23 -39
- package/es/index.js +2 -1
- package/lib/Component.js +449 -0
- package/lib/Emitter.js +29 -11
- package/lib/Model.js +35 -26
- package/lib/View.js +23 -39
- package/lib/index.js +2 -0
- package/package.json +4 -2
- package/src/Component.js +434 -0
- package/src/Emitter.js +29 -11
- package/src/Model.js +35 -26
- package/src/View.js +23 -39
- package/src/index.js +2 -1
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
|
|
20
|
-
*
|
|
21
|
-
* Models must be unaware of views.
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
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
|
-
*
|
|
35
|
-
*
|
|
36
|
+
* import { View } from 'rasti';
|
|
37
|
+
*
|
|
38
|
+
* class Timer extends View {
|
|
36
39
|
* constructor(options) {
|
|
37
40
|
* super(options);
|
|
38
|
-
* //
|
|
39
|
-
* this.
|
|
40
|
-
* // Listen to model
|
|
41
|
-
* this.model.on('change:
|
|
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
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
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
|
-
*
|
|
54
|
-
*
|
|
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
|
/**
|