rasti 2.0.0-alpha.8 → 2.0.0
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 +25 -14
- package/dist/rasti.js +4 -4
- package/dist/rasti.min.js +1 -1
- package/es/Component.js +61 -51
- package/es/Emitter.js +2 -2
- package/es/Model.js +60 -21
- package/es/View.js +13 -10
- package/lib/Component.js +62 -53
- package/lib/Emitter.js +4 -5
- package/lib/Model.js +62 -24
- package/lib/View.js +14 -12
- package/package.json +34 -34
- package/src/Component.js +63 -50
- package/src/Emitter.js +2 -2
- package/src/Model.js +55 -17
- package/src/View.js +11 -7
package/src/Component.js
CHANGED
|
@@ -47,9 +47,9 @@ const evalExpression = (expression, context, ...args) =>
|
|
|
47
47
|
|
|
48
48
|
/**
|
|
49
49
|
* Components are a special kind of `View` that is designed to be easily composable,
|
|
50
|
-
* making it simple to add child views and build complex user interfaces
|
|
50
|
+
* making it simple to add child views and build complex user interfaces.
|
|
51
51
|
* Unlike views, which are render-agnostic, components have a specific set of rendering
|
|
52
|
-
* guidelines that allow for a more declarative development style
|
|
52
|
+
* guidelines that allow for a more declarative development style.
|
|
53
53
|
* Components are defined with the `create` static method, which takes a tagged template.
|
|
54
54
|
* @module
|
|
55
55
|
* @extends Rasti.View
|
|
@@ -74,25 +74,19 @@ const evalExpression = (expression, context, ...args) =>
|
|
|
74
74
|
*/
|
|
75
75
|
export default class Component extends View {
|
|
76
76
|
constructor(options = {}) {
|
|
77
|
-
super(
|
|
77
|
+
super(...arguments);
|
|
78
78
|
// Extend "this" with options, mapping componentOptions keys.
|
|
79
79
|
Object.keys(options).forEach(key => {
|
|
80
80
|
if (componentOptions[key]) this[key] = options[key];
|
|
81
81
|
});
|
|
82
82
|
// Store options by default.
|
|
83
83
|
this.options = options;
|
|
84
|
-
// Ensure id.
|
|
85
|
-
this.id = this.attributes.id ?
|
|
86
|
-
// If id is provided, evaluate it.
|
|
87
|
-
evalExpression(this.attributes.id, this, this) :
|
|
88
|
-
// Generate a unique id and set it as id attribute.
|
|
89
|
-
Component.ID_TEMPLATE(this.uid);
|
|
90
84
|
// Bind onChange to this to be used as listener.
|
|
91
85
|
// Store bound version, so it can be removed on onDestroy method.
|
|
92
86
|
this.onChange = this.onChange.bind(this);
|
|
93
87
|
// Listen to model changes and call onChange.
|
|
94
|
-
if (this.model) this.model.on('change', this.onChange);
|
|
95
|
-
if (this.state) this.state.on('change', this.onChange);
|
|
88
|
+
if (this.model && this.model.on) this.model.on('change', this.onChange);
|
|
89
|
+
if (this.state && this.state.on) this.state.on('change', this.onChange);
|
|
96
90
|
// Call life cycle method.
|
|
97
91
|
this.onCreate.apply(this, arguments);
|
|
98
92
|
}
|
|
@@ -103,7 +97,18 @@ export default class Component extends View {
|
|
|
103
97
|
*/
|
|
104
98
|
ensureElement() {
|
|
105
99
|
// If el is provided, delegate events.
|
|
106
|
-
if (this.el)
|
|
100
|
+
if (this.el) {
|
|
101
|
+
this.delegateEvents();
|
|
102
|
+
this.id = this.el.id;
|
|
103
|
+
}
|
|
104
|
+
// Ensure id.
|
|
105
|
+
if (!this.id) {
|
|
106
|
+
this.id = this.attributes && this.attributes.id ?
|
|
107
|
+
// If id is provided, evaluate it.
|
|
108
|
+
evalExpression(this.attributes.id, this, this) :
|
|
109
|
+
// Generate a unique id and set it as id attribute.
|
|
110
|
+
Component.ID_TEMPLATE(this.uid);
|
|
111
|
+
}
|
|
107
112
|
}
|
|
108
113
|
|
|
109
114
|
/*
|
|
@@ -117,33 +122,32 @@ export default class Component extends View {
|
|
|
117
122
|
* Eval attributes expressions.
|
|
118
123
|
*/
|
|
119
124
|
getAttributes() {
|
|
120
|
-
const add = {};
|
|
125
|
+
const add = { id : this.id };
|
|
121
126
|
const remove = {};
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
add[key] = value;
|
|
139
|
-
attrs.push(`${key}="${value}"`);
|
|
140
|
-
}
|
|
141
|
-
});
|
|
127
|
+
const html = [`id="${this.id}"`];
|
|
128
|
+
|
|
129
|
+
if (this.attributes) {
|
|
130
|
+
Object.keys(this.attributes).forEach(key => {
|
|
131
|
+
if (key === 'id') return;
|
|
132
|
+
// Evaluate attribute value.
|
|
133
|
+
let value = evalExpression(this.attributes[key], this, this);
|
|
134
|
+
|
|
135
|
+
// Transform bool attribute values
|
|
136
|
+
if (value === false) {
|
|
137
|
+
remove[key] = true;
|
|
138
|
+
} else if (value === true) {
|
|
139
|
+
add[key] = '';
|
|
140
|
+
html.push(key);
|
|
141
|
+
} else {
|
|
142
|
+
if (value === null || typeof value === 'undefined') value = '';
|
|
142
143
|
|
|
143
|
-
|
|
144
|
-
|
|
144
|
+
add[key] = value;
|
|
145
|
+
html.push(`${key}="${value}"`);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
145
149
|
|
|
146
|
-
return { add, remove, html :
|
|
150
|
+
return { add, remove, html : html.join(' ') };
|
|
147
151
|
}
|
|
148
152
|
|
|
149
153
|
/*
|
|
@@ -177,8 +181,8 @@ export default class Component extends View {
|
|
|
177
181
|
super.destroy.apply(this, arguments);
|
|
178
182
|
// Stop listening to `change`.
|
|
179
183
|
// Set destroyed flag to prevent a last render after destroyed. TODO: Review
|
|
180
|
-
if (this.model) this.model.off('change', this.onChange);
|
|
181
|
-
if (this.state) this.state.off('change', this.onChange);
|
|
184
|
+
if (this.model && this.model.off) this.model.off('change', this.onChange);
|
|
185
|
+
if (this.state && this.state.off) this.state.off('change', this.onChange);
|
|
182
186
|
this.destroyed = true;
|
|
183
187
|
}
|
|
184
188
|
|
|
@@ -195,8 +199,8 @@ export default class Component extends View {
|
|
|
195
199
|
* Maybe comparing new attributes with previous ones and calling
|
|
196
200
|
* render when needed. Or doing some dom transformation.
|
|
197
201
|
* @param model {Rasti.Model} The model that emitted the event.
|
|
198
|
-
* @param
|
|
199
|
-
* @param
|
|
202
|
+
* @param changed {object} Object containing keys and values that has changed.
|
|
203
|
+
* @param [...args] {any} Any extra arguments passed to set method.
|
|
200
204
|
*/
|
|
201
205
|
onChange() {
|
|
202
206
|
this.render();
|
|
@@ -257,15 +261,21 @@ export default class Component extends View {
|
|
|
257
261
|
toString() {
|
|
258
262
|
// Normally there won't be any children, but if there are, destroy them.
|
|
259
263
|
this.destroyChildren();
|
|
260
|
-
//
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
264
|
+
// Get tag name.
|
|
265
|
+
const tag = this.tag || 'div';
|
|
266
|
+
// Get attributes.
|
|
267
|
+
const attributes = this.getAttributes().html;
|
|
268
|
+
// Replace expressions of inner template.
|
|
269
|
+
const inner = this.template &&
|
|
270
|
+
this.template.inner &&
|
|
271
|
+
this.replaceExpressions(this.template.inner, (component) => {
|
|
272
|
+
// Add child component.
|
|
273
|
+
return this.addChild(component);
|
|
274
|
+
});
|
|
265
275
|
// Generate outer template.
|
|
266
276
|
return inner ?
|
|
267
|
-
`<${
|
|
268
|
-
`<${
|
|
277
|
+
`<${tag} ${attributes}>${inner}</${tag}>` :
|
|
278
|
+
`<${tag} ${attributes} />`;
|
|
269
279
|
}
|
|
270
280
|
|
|
271
281
|
/*
|
|
@@ -290,7 +300,7 @@ export default class Component extends View {
|
|
|
290
300
|
this.el.setAttribute(key, attributes.add[key]);
|
|
291
301
|
});
|
|
292
302
|
// Check for `template.inner` to see if view has innerHTML.
|
|
293
|
-
if (this.template.inner) {
|
|
303
|
+
if (this.template && this.template.inner) {
|
|
294
304
|
const previousChildren = this.children;
|
|
295
305
|
|
|
296
306
|
this.children = [];
|
|
@@ -308,8 +318,10 @@ export default class Component extends View {
|
|
|
308
318
|
);
|
|
309
319
|
|
|
310
320
|
if (found) {
|
|
321
|
+
const tag = found.el.tagName.toLowerCase();
|
|
322
|
+
const id = found.el.id;
|
|
311
323
|
// If child already exists, replace it html by its root element.
|
|
312
|
-
out = `<${
|
|
324
|
+
out = `<${tag} id="${id}"></${tag}>`;
|
|
313
325
|
// Add child to recycled children.
|
|
314
326
|
recycledChildren.push(found);
|
|
315
327
|
// Destroy new child component. Use recycled one instead.
|
|
@@ -374,7 +386,7 @@ export default class Component extends View {
|
|
|
374
386
|
* @param {boolean} hydrate If true, the view will use existing html.
|
|
375
387
|
* @return {Rasti.Component}
|
|
376
388
|
*/
|
|
377
|
-
static mount(options
|
|
389
|
+
static mount(options, el, hydrate) {
|
|
378
390
|
// Instantiate view.
|
|
379
391
|
const view = new this(options);
|
|
380
392
|
// If `el` is passed, mount component.
|
|
@@ -393,6 +405,7 @@ export default class Component extends View {
|
|
|
393
405
|
// Return view instance.
|
|
394
406
|
return view;
|
|
395
407
|
}
|
|
408
|
+
|
|
396
409
|
/**
|
|
397
410
|
* Takes a tagged template containing an HTML string,
|
|
398
411
|
* and returns a new `Component` class.
|
package/src/Emitter.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `Emitter` is a class that provides an easy way to implement the observer pattern
|
|
3
|
-
* in your applications
|
|
4
|
-
* It can be extended to create new classes that have the ability to emit and bind custom named events
|
|
3
|
+
* in your applications.
|
|
4
|
+
* It can be extended to create new classes that have the ability to emit and bind custom named events.
|
|
5
5
|
* Emitter is used by `Model` and `View` classes, which inherit from it to implement
|
|
6
6
|
* event-driven functionality.
|
|
7
7
|
*
|
package/src/Model.js
CHANGED
|
@@ -3,10 +3,10 @@ import Emitter from './Emitter.js';
|
|
|
3
3
|
* - Orchestrates data and business logic.
|
|
4
4
|
* - Emits events when data changes.
|
|
5
5
|
*
|
|
6
|
-
* A `Model` manages an internal table of data attributes and triggers change events when any of its data is modified
|
|
6
|
+
* A `Model` manages an internal table of data attributes and triggers change events when any of its data is modified.
|
|
7
7
|
* Models may handle syncing data with a persistence layer. To design your models, create atomic, reusable objects
|
|
8
|
-
* that contain all the necessary functions for manipulating their specific data
|
|
9
|
-
* Models should be easily passed throughout your app and used anywhere the corresponding data is needed
|
|
8
|
+
* that contain all the necessary functions for manipulating their specific data.
|
|
9
|
+
* Models should be easily passed throughout your app and used anywhere the corresponding data is needed.
|
|
10
10
|
* Rasti models stores its attributes in `this.attributes`, which is extended from `this.defaults` and the
|
|
11
11
|
* constructor `attrs` parameter. For every attribute, a getter is generated to retrieve the model property
|
|
12
12
|
* from `this.attributes`, and a setter is created to set the model property in `this.attributes` and emit `change`
|
|
@@ -90,30 +90,68 @@ export default class Model extends Emitter {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
|
-
* Set an attribute into `this.attributes`.
|
|
94
|
-
* Emit `change` and `change:attribute` if value change.
|
|
93
|
+
* Set an attribute into `this.attributes`.
|
|
94
|
+
* Emit `change` and `change:attribute` if a value change.
|
|
95
95
|
* Could be called in two forms, `this.set('key', value)` and
|
|
96
|
-
* `this.set({ key : value })`.
|
|
97
|
-
* This method is called internally by generated setters.
|
|
96
|
+
* `this.set({ key : value })`.
|
|
97
|
+
* This method is called internally by generated setters.
|
|
98
|
+
* The `change` event listener will receive the model instance, an object containing the changed attributes, and the rest of the arguments passed to `set` method.
|
|
99
|
+
* The `change:attribute` event listener will receive the model instance, the new attribute value, and the rest of the arguments passed to `set` method.
|
|
98
100
|
* @param {string} key Attribute key or object containing keys/values.
|
|
99
101
|
* @param [value] Attribute value.
|
|
100
102
|
* @return {this} This model.
|
|
101
103
|
* @emits change
|
|
102
104
|
* @emits change:attribute
|
|
103
105
|
*/
|
|
104
|
-
set(key, value) {
|
|
105
|
-
let attrs
|
|
106
|
+
set(key, value, ...rest) {
|
|
107
|
+
let attrs, args;
|
|
108
|
+
// Handle both `"key", value` and `{key: value}` style arguments.
|
|
109
|
+
if (typeof key === 'object') {
|
|
110
|
+
attrs = key;
|
|
111
|
+
args = [value, ...rest];
|
|
112
|
+
} else {
|
|
113
|
+
attrs = { [key] : value };
|
|
114
|
+
args = rest;
|
|
115
|
+
}
|
|
116
|
+
// Are we in a nested `set` call?
|
|
117
|
+
// Calling a `set` inside a `change:attribute` or `change` event listener
|
|
118
|
+
const changing = this._changing;
|
|
119
|
+
this._changing = true;
|
|
120
|
+
// Store changed attributes.
|
|
121
|
+
const changed = {};
|
|
122
|
+
// Store previous attributes.
|
|
123
|
+
if (!changing) {
|
|
124
|
+
this.previous = Object.assign({}, this.attributes);
|
|
125
|
+
}
|
|
126
|
+
// Set attributes.
|
|
106
127
|
Object.keys(attrs).forEach(key => {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
// Emit change events.
|
|
112
|
-
if (changed) {
|
|
113
|
-
this.emit('change', this, key, attrs[key]);
|
|
114
|
-
this.emit(`change:${key}`, this, attrs[key]);
|
|
128
|
+
// Use equality to determine if value changed.
|
|
129
|
+
if (attrs[key] !== this.attributes[key]) {
|
|
130
|
+
changed[key] = attrs[key];
|
|
131
|
+
this.attributes[key] = attrs[key];
|
|
115
132
|
}
|
|
116
133
|
});
|
|
134
|
+
|
|
135
|
+
const changedKeys = Object.keys(changed);
|
|
136
|
+
// Pending `change` event arguments.
|
|
137
|
+
if (changedKeys.length) this._pending = ['change', this, changed, ...args];
|
|
138
|
+
// Emit `change:attribute` events.
|
|
139
|
+
changedKeys.forEach(key => {
|
|
140
|
+
this.emit(`change:${key}`, this, attrs[key], ...args);
|
|
141
|
+
});
|
|
142
|
+
// Don't emit `change` event until the end of the nested
|
|
143
|
+
// `set` calls inside `change:attribute` event listeners.
|
|
144
|
+
if (changing) return this;
|
|
145
|
+
// Emit `change` events, that might be nested.
|
|
146
|
+
while(this._pending) {
|
|
147
|
+
const pendingChange = this._pending;
|
|
148
|
+
this._pending = null;
|
|
149
|
+
this.emit.apply(this, pendingChange);
|
|
150
|
+
}
|
|
151
|
+
// Reset flags.
|
|
152
|
+
this._pending = null;
|
|
153
|
+
this._changing = false;
|
|
154
|
+
|
|
117
155
|
return this;
|
|
118
156
|
}
|
|
119
157
|
|
package/src/View.js
CHANGED
|
@@ -17,10 +17,10 @@ const viewOptions = {
|
|
|
17
17
|
* - Sends captured input to the model.
|
|
18
18
|
*
|
|
19
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
|
|
20
|
+
* However, views can also be independent and have no associated data.
|
|
21
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
|
|
23
|
-
* Each `View` has a root element, `this.el`, which is used for event delegation
|
|
22
|
+
* emitted by the models to re-render themselves based on changes.
|
|
23
|
+
* Each `View` has a root element, `this.el`, which is used for event delegation.
|
|
24
24
|
* All element lookups are scoped to this element, and any rendering or DOM manipulations should be done inside it.
|
|
25
25
|
* If `this.el` is not present, an element will be created using `this.tag` (defaulting to div) and `this.attributes`.
|
|
26
26
|
* @module
|
|
@@ -104,6 +104,7 @@ export default class View extends Emitter {
|
|
|
104
104
|
/**
|
|
105
105
|
* Destroy the view.
|
|
106
106
|
* Destroy children views if any, undelegate events, stop listening to events, call `onDestroy` lifecycle method.
|
|
107
|
+
* @return {Rasti.View} Return `this` for chaining.
|
|
107
108
|
*/
|
|
108
109
|
destroy() {
|
|
109
110
|
// Call destroy on children.
|
|
@@ -180,18 +181,21 @@ export default class View extends Emitter {
|
|
|
180
181
|
|
|
181
182
|
/**
|
|
182
183
|
* Remove `this.el` from DOM.
|
|
184
|
+
* @return {Rasti.View} Return `this` for chaining.
|
|
183
185
|
*/
|
|
184
186
|
removeElement() {
|
|
185
187
|
this.el.parentNode.removeChild(this.el);
|
|
188
|
+
// Return `this` for chaining.
|
|
189
|
+
return this;
|
|
186
190
|
}
|
|
187
191
|
|
|
188
192
|
/**
|
|
189
|
-
* Provide declarative listeners for DOM events within a view. If an events hash is not passed directly, uses `this.events` as the source
|
|
193
|
+
* Provide declarative listeners for DOM events within a view. If an events hash is not passed directly, uses `this.events` as the source.
|
|
190
194
|
* Events are written in the format `{'event selector' : 'listener'}`. The listener may be either the name of a method on the view, or a direct function body.
|
|
191
|
-
* Omitting the selector causes the event to be bound to the view's root element (`this.el`)
|
|
195
|
+
* Omitting the selector causes the event to be bound to the view's root element (`this.el`).
|
|
192
196
|
* By default, `delegateEvents` is called within the View's constructor,
|
|
193
|
-
* so if you have a simple events hash, all of your DOM events will always already be connected, and you will never have to call this function yourself.
|
|
194
|
-
* All attached listeners are bound to the view automatically, so when the listeners are invoked, `this` continues to refer to the view object
|
|
197
|
+
* so if you have a simple events hash, all of your DOM events will always already be connected, and you will never have to call this function yourself.
|
|
198
|
+
* All attached listeners are bound to the view automatically, so when the listeners are invoked, `this` continues to refer to the view object.
|
|
195
199
|
* When `delegateEvents` is run again, perhaps with a different events hash, all listeners are removed and delegated afresh.
|
|
196
200
|
* @param {object} [events] Object in the format `{'event selector' : 'listener'}`. Used to bind delegated event listeners to root element.
|
|
197
201
|
* @return {Rasti.View} Return `this` for chaining.
|