rasti 2.0.0-alpha.4 → 2.0.0-alpha.6

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
@@ -62,7 +62,7 @@ export default class View extends Emitter {
62
62
  // Useful to generate elements ids.
63
63
  this.uid = `uid${++View.uid}`;
64
64
  // Store delegated events listeners,
65
- // so they can be unbinded later.
65
+ // so they can be unbound later.
66
66
  this.delegatedEventListeners = [];
67
67
  // Store child views,
68
68
  // so they can be destroyed.
@@ -104,25 +104,24 @@ 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
- * Pass options.remove as true to remove the view's root element (`this.el`) from the DOM.
108
- * @param {object} options.remove Remove the view's root element (`this.el`) from the DOM.
109
107
  */
110
- destroy({ remove } = {}) {
108
+ destroy() {
111
109
  // Call destroy on children.
112
110
  this.destroyChildren();
113
111
  // Undelegate `this.el` event listeners
114
112
  this.undelegateEvents();
115
113
  // Unbind `this` events.
116
114
  this.off();
117
- // Remove `this.el` if "options.remove" is true.
118
- if (remove) this.removeElement();
119
115
  // Call onDestroy lifecycle method
120
116
  this.onDestroy.apply(this, arguments);
117
+ // Return `this` for chaining.
118
+ return this;
121
119
  }
122
120
 
123
121
  /**
124
122
  * `onDestroy` lifecycle method is called after view is destroyed.
125
123
  * Override with your code. Useful to stop listening to model's events.
124
+ * @param {object} options Options object or any arguments passed to `destroy` method.
126
125
  */
127
126
  onDestroy() {}
128
127
 
@@ -211,13 +210,15 @@ export default class View extends Emitter {
211
210
  let eventTypes = {};
212
211
 
213
212
  Object.keys(events).forEach(key => {
214
- let keyParts = key.split(' '),
215
- type = keyParts.shift(),
216
- selector = keyParts.join(' '),
217
- listener = events[key];
213
+ const keyParts = key.split(' ');
214
+ const type = keyParts.shift();
215
+ const selector = keyParts.join(' ');
218
216
 
217
+ let listener = events[key];
218
+ // Listener may be a string representing a method name on the view,
219
+ // or a function.
219
220
  listener = (
220
- typeof listener === 'string' ?
221
+ typeof listener === 'string' ?
221
222
  this[listener] :
222
223
  listener
223
224
  ).bind(this);
@@ -227,22 +228,19 @@ export default class View extends Emitter {
227
228
  eventTypes[type].push({ selector, listener });
228
229
  });
229
230
 
230
- const indexOf = Array.prototype.indexOf;
231
-
232
231
  Object.keys(eventTypes).forEach(type => {
233
- let self = this;
234
- let typeListener = function (event) {
235
- eventTypes[type].forEach(function ({ selector, listener }) {
236
- if (indexOf.call(selector ? self.el.querySelectorAll(selector) : [self.el], event.target) > -1) {
237
- listener(event, self);
238
- }
232
+ // Listener for the type of event.
233
+ const typeListener = (event) => {
234
+ // Iterate and run every individual listener if the selector matches.
235
+ eventTypes[type].forEach(({ selector, listener }) => {
236
+ if (!selector || event.target.closest(selector)) listener(event, this);
239
237
  });
240
238
  };
241
239
 
242
240
  this.delegatedEventListeners.push({ type, listener : typeListener });
243
241
  this.el.addEventListener(type, typeListener);
244
242
  });
245
-
243
+ // Return `this` for chaining.
246
244
  return this;
247
245
  }
248
246
 
@@ -256,7 +254,7 @@ export default class View extends Emitter {
256
254
  });
257
255
 
258
256
  this.delegatedEventListeners = [];
259
-
257
+ // Return `this` for chaining.
260
258
  return this;
261
259
  }
262
260
 
@@ -271,6 +269,7 @@ export default class View extends Emitter {
271
269
  */
272
270
  render() {
273
271
  if (this.template) this.el.innerHTML = this.template(this.model);
272
+ // Return `this` for chaining.
274
273
  return this;
275
274
  }
276
275
  }