rasti 1.1.1 → 1.1.3

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/Emitter.js CHANGED
@@ -1,9 +1,7 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
-
5
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
-
4
+ exports["default"] = void 0;
7
5
  /**
8
6
  * Emitter is a class that can be extended giving the subclass the ability to emit
9
7
  * and bind custom named events. Model and View inherits from it.
@@ -23,109 +21,90 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
23
21
  * // Add event listener to ready event.
24
22
  * app.on('ready', () => console.log('app is ready!'));
25
23
  */
26
- var Emitter = function () {
27
- function Emitter() {
28
- _classCallCheck(this, Emitter);
24
+ var Emitter = /*#__PURE__*/function () {
25
+ function Emitter() {}
26
+ var _proto = Emitter.prototype;
27
+ /**
28
+ * Adds event listener.
29
+ * @param {string} type Type of the event (e.g. `change`).
30
+ * @param {function} listener Callback function to be called when the event is emitted.
31
+ * @example
32
+ * this.model.on('change', this.render.bind(this)); // Re render when model changes.
33
+ */
34
+ _proto.on = function on(type, listener) {
35
+ if (typeof listener !== 'function') {
36
+ throw TypeError('Listener must be a function');
29
37
  }
30
-
31
- /**
32
- * Adds event listener.
33
- * @param {string} type Type of the event (e.g. `change`).
34
- * @param {function} listener Callback function to be called when the event is emitted.
35
- * @example
36
- * this.model.on('change', this.render.bind(this)); // Re render when model changes.
37
- */
38
- Emitter.prototype.on = function on(type, listener) {
39
- if (typeof listener !== 'function') {
40
- throw TypeError('Listener must be a function');
41
- }
42
-
43
- if (!this.listeners) this.listeners = {};
44
- if (!this.listeners[type]) this.listeners[type] = [];
45
-
46
- this.listeners[type].push(listener);
47
- };
48
-
49
- /**
50
- * Adds event listener that executes once.
51
- * @param {string} type Type of the event (e.g. `change`).
52
- * @param {function} listener Callback function to be called when the event is emitted.
53
- * @example
54
- * this.model.once('change', () => console.log('This will happen once'));
55
- */
56
-
57
-
58
- Emitter.prototype.once = function once(type, _listener2) {
59
- if (typeof _listener2 === 'function') {
60
- var self = this;
61
- var _listener = _listener2;
62
-
63
- _listener2 = function listener() {
64
- _listener.apply(undefined, arguments);
65
- self.off(type, _listener2);
66
- };
67
- }
68
- this.on(type, _listener2);
69
- };
70
-
71
- /**
72
- * Removes event listeners.
73
- * @param {string} [type] Type of the event (e.g. `change`). If is not provided, it removes all listeners.
74
- * @param {function} [listener] Callback function to be called when the event is emitted. If listener is not provided, it removes all listeners for specified type.
75
- * @example
76
- * this.model.off('change'); // Stop listening to changes.
77
- */
78
-
79
-
80
- Emitter.prototype.off = function off(type, listener) {
81
- if (!type) {
82
- this.listeners = {};
83
- } else {
84
- if (!listener) {
85
- delete this.listeners[type];
86
- } else {
87
- var listeners = this.listeners[type];
88
- if (listeners) {
89
- var copy = listeners.slice();
90
-
91
- copy.forEach(function (fn, idx) {
92
- if (fn === listener) listeners.splice(idx, 1);
93
- });
94
-
95
- if (!listeners.length) {
96
- delete this.listeners[type];
97
- }
98
- }
99
- }
100
- }
101
- };
102
-
103
- /**
104
- * Emits event of specified type. Listeners will receive specified arguments.
105
- * @param {string} type Type of the event (e.g. `change`).
106
- * @param {any} [...args] Arguments to be passed to listener.
107
- * @example
108
- * this.emit('invalid'); // Emit validation error event.
109
- */
110
-
111
-
112
- Emitter.prototype.emit = function emit(type) {
113
- for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
114
- args[_key - 1] = arguments[_key];
38
+ if (!this.listeners) this.listeners = {};
39
+ if (!this.listeners[type]) this.listeners[type] = [];
40
+ this.listeners[type].push(listener);
41
+ }
42
+
43
+ /**
44
+ * Adds event listener that executes once.
45
+ * @param {string} type Type of the event (e.g. `change`).
46
+ * @param {function} listener Callback function to be called when the event is emitted.
47
+ * @example
48
+ * this.model.once('change', () => console.log('This will happen once'));
49
+ */;
50
+ _proto.once = function once(type, _listener2) {
51
+ if (typeof _listener2 === 'function') {
52
+ var self = this;
53
+ var _listener = _listener2;
54
+ _listener2 = function listener() {
55
+ _listener.apply(void 0, arguments);
56
+ self.off(type, _listener2);
57
+ };
58
+ }
59
+ this.on(type, _listener2);
60
+ }
61
+
62
+ /**
63
+ * Removes event listeners.
64
+ * @param {string} [type] Type of the event (e.g. `change`). If is not provided, it removes all listeners.
65
+ * @param {function} [listener] Callback function to be called when the event is emitted. If listener is not provided, it removes all listeners for specified type.
66
+ * @example
67
+ * this.model.off('change'); // Stop listening to changes.
68
+ */;
69
+ _proto.off = function off(type, listener) {
70
+ if (!type) {
71
+ this.listeners = {};
72
+ } else {
73
+ if (!listener) {
74
+ delete this.listeners[type];
75
+ } else {
76
+ var listeners = this.listeners[type];
77
+ if (listeners) {
78
+ var copy = listeners.slice();
79
+ copy.forEach(function (fn, idx) {
80
+ if (fn === listener) listeners.splice(idx, 1);
81
+ });
82
+ if (!listeners.length) {
83
+ delete this.listeners[type];
84
+ }
115
85
  }
116
-
117
- var listeners = this.listeners && this.listeners[type];
118
-
119
- if (!listeners || !listeners.length) return;
120
-
121
- var copy = listeners.slice();
122
-
123
- copy.forEach(function (fn) {
124
- fn.apply(undefined, args);
125
- });
126
- };
127
-
128
- return Emitter;
86
+ }
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Emits event of specified type. Listeners will receive specified arguments.
92
+ * @param {string} type Type of the event (e.g. `change`).
93
+ * @param {any} [...args] Arguments to be passed to listener.
94
+ * @example
95
+ * this.emit('invalid'); // Emit validation error event.
96
+ */;
97
+ _proto.emit = function emit(type) {
98
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
99
+ args[_key - 1] = arguments[_key];
100
+ }
101
+ var listeners = this.listeners && this.listeners[type];
102
+ if (!listeners || !listeners.length) return;
103
+ var copy = listeners.slice();
104
+ copy.forEach(function (fn) {
105
+ fn.apply(void 0, args);
106
+ });
107
+ };
108
+ return Emitter;
129
109
  }();
130
-
131
- exports['default'] = Emitter;
110
+ exports["default"] = Emitter;
package/lib/Model.js CHANGED
@@ -1,19 +1,12 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
-
5
- var _Emitter2 = require('./Emitter');
6
-
7
- var _Emitter3 = _interopRequireDefault(_Emitter2);
8
-
9
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
10
-
11
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
-
13
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
14
-
15
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
16
-
4
+ exports["default"] = void 0;
5
+ var _Emitter2 = _interopRequireDefault(require("./Emitter"));
6
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
7
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
8
+ function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
9
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
17
10
  /**
18
11
  * - Orchestrates data and business logic.
19
12
  * - Emits events when data changes.
@@ -50,113 +43,95 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function"
50
43
  * // Complete todo.
51
44
  * todo.toggle(); // Completed: true
52
45
  */
53
- var Model = function (_Emitter) {
54
- _inherits(Model, _Emitter);
55
-
56
- function Model() {
57
- var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
58
-
59
- _classCallCheck(this, Model);
60
-
61
- // Call preinitialize.
62
- var _this = _possibleConstructorReturn(this, _Emitter.call(this));
63
-
64
- _this.preinitialize.apply(_this, arguments);
65
- // attributes object.
66
- _this.attributes = Object.assign({}, _this.defaults || {}, attrs);
67
- // Previous attributes.
68
- _this.previous = {};
69
- // Generate getters/setters for every attr.
70
- Object.keys(_this.attributes).forEach(_this.defineAttribute.bind(_this));
71
- return _this;
72
- }
73
-
74
- /**
75
- * If you define a preinitialize method, it will be invoked when the Model is first created, before any instantiation logic is run for the Model.
76
- * @param {object} attrs Object containing model attributes to extend `this.attributes`.
77
- */
78
-
79
-
80
- Model.prototype.preinitialize = function preinitialize() {};
81
-
82
- /**
83
- * Generate getter/setter for the given key. In order to emit `change` events.
84
- * This method is called internally by the constructor
85
- * for `this.attributes`.
86
- * @param {string} key Attribute key.
87
- */
88
-
89
-
90
- Model.prototype.defineAttribute = function defineAttribute(key) {
91
- var _this2 = this;
92
-
93
- Object.defineProperty(this, key, {
94
- get: function get() {
95
- return _this2.get(key);
96
- },
97
- set: function set(value) {
98
- _this2.set(key, value);
99
- }
100
- });
101
- };
102
-
103
- /**
104
- * Get an attribute from `this.attributes`.
105
- * This method is called internally by generated getters.
106
- * @param {string} key Attribute key.
107
- * @return {any} The attribute value.
108
- */
109
-
110
-
111
- Model.prototype.get = function get(key) {
112
- return this.attributes[key];
113
- };
114
-
115
- /**
116
- * Set an attribute into `this.attributes`.
117
- * Emit `change` and `change:attribute` if value change.
118
- * Could be called in two forms, `this.set('key', value)` and
119
- * `this.set({ key : value })`.
120
- * This method is called internally by generated setters.
121
- * @param {string} key Attribute key or object containing keys/values.
122
- * @param [value] Attribute value.
123
- * @return {this} This model.
124
- * @emits change
125
- * @emits change:attribute
126
- */
127
-
128
-
129
- Model.prototype.set = function set(key, value) {
130
- var _ref,
131
- _this3 = this;
132
-
133
- var attrs = typeof key === 'object' ? key : (_ref = {}, _ref[key] = value, _ref);
134
- Object.keys(attrs).forEach(function (key) {
135
- var changed = key in _this3.attributes && attrs[key] !== _this3.attributes[key];
136
-
137
- _this3.previous[key] = _this3.attributes[key];
138
- _this3.attributes[key] = attrs[key];
139
- // Emit change events.
140
- if (changed) {
141
- _this3.emit('change', _this3, key, attrs[key]);
142
- _this3.emit('change:' + key, _this3, attrs[key]);
143
- }
144
- });
145
- return this;
146
- };
147
-
148
- /**
149
- * Return object representation of the model to be used for JSON serialization.
150
- * By default returns `this.attributes`.
151
- * @return {object} Object representation of the model to be used for JSON serialization.
152
- */
153
-
154
-
155
- Model.prototype.toJSON = function toJSON() {
156
- return this.attributes;
157
- };
158
-
159
- return Model;
160
- }(_Emitter3['default']);
161
-
162
- exports['default'] = Model;
46
+ var Model = /*#__PURE__*/function (_Emitter) {
47
+ _inheritsLoose(Model, _Emitter);
48
+ function Model() {
49
+ var _this;
50
+ var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
51
+ _this = _Emitter.call(this) || this;
52
+ // Call preinitialize.
53
+ _this.preinitialize.apply(_assertThisInitialized(_this), arguments);
54
+ // attributes object.
55
+ _this.attributes = Object.assign({}, _this.defaults || {}, attrs);
56
+ // Previous attributes.
57
+ _this.previous = {};
58
+ // Generate getters/setters for every attr.
59
+ Object.keys(_this.attributes).forEach(_this.defineAttribute.bind(_assertThisInitialized(_this)));
60
+ return _this;
61
+ }
62
+
63
+ /**
64
+ * If you define a preinitialize method, it will be invoked when the Model is first created, before any instantiation logic is run for the Model.
65
+ * @param {object} attrs Object containing model attributes to extend `this.attributes`.
66
+ */
67
+ var _proto = Model.prototype;
68
+ _proto.preinitialize = function preinitialize() {}
69
+
70
+ /**
71
+ * Generate getter/setter for the given key. In order to emit `change` events.
72
+ * This method is called internally by the constructor
73
+ * for `this.attributes`.
74
+ * @param {string} key Attribute key.
75
+ */;
76
+ _proto.defineAttribute = function defineAttribute(key) {
77
+ var _this2 = this;
78
+ Object.defineProperty(this, key, {
79
+ get: function get() {
80
+ return _this2.get(key);
81
+ },
82
+ set: function set(value) {
83
+ _this2.set(key, value);
84
+ }
85
+ });
86
+ }
87
+
88
+ /**
89
+ * Get an attribute from `this.attributes`.
90
+ * This method is called internally by generated getters.
91
+ * @param {string} key Attribute key.
92
+ * @return {any} The attribute value.
93
+ */;
94
+ _proto.get = function get(key) {
95
+ return this.attributes[key];
96
+ }
97
+
98
+ /**
99
+ * Set an attribute into `this.attributes`.
100
+ * Emit `change` and `change:attribute` if value change.
101
+ * Could be called in two forms, `this.set('key', value)` and
102
+ * `this.set({ key : value })`.
103
+ * This method is called internally by generated setters.
104
+ * @param {string} key Attribute key or object containing keys/values.
105
+ * @param [value] Attribute value.
106
+ * @return {this} This model.
107
+ * @emits change
108
+ * @emits change:attribute
109
+ */;
110
+ _proto.set = function set(key, value) {
111
+ var _ref,
112
+ _this3 = this;
113
+ var attrs = typeof key === 'object' ? key : (_ref = {}, _ref[key] = value, _ref);
114
+ Object.keys(attrs).forEach(function (key) {
115
+ var changed = key in _this3.attributes && attrs[key] !== _this3.attributes[key];
116
+ _this3.previous[key] = _this3.attributes[key];
117
+ _this3.attributes[key] = attrs[key];
118
+ // Emit change events.
119
+ if (changed) {
120
+ _this3.emit('change', _this3, key, attrs[key]);
121
+ _this3.emit("change:" + key, _this3, attrs[key]);
122
+ }
123
+ });
124
+ return this;
125
+ }
126
+
127
+ /**
128
+ * Return object representation of the model to be used for JSON serialization.
129
+ * By default returns `this.attributes`.
130
+ * @return {object} Object representation of the model to be used for JSON serialization.
131
+ */;
132
+ _proto.toJSON = function toJSON() {
133
+ return this.attributes;
134
+ };
135
+ return Model;
136
+ }(_Emitter2["default"]);
137
+ exports["default"] = Model;