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/README.md +5 -4
- package/dist/rasti.js +56 -116
- package/dist/rasti.min.js +1 -1
- package/es/Emitter.js +84 -105
- package/es/Model.js +94 -116
- package/es/View.js +237 -289
- package/es/index.js +3 -4
- package/lib/Emitter.js +86 -107
- package/lib/Model.js +99 -124
- package/lib/View.js +242 -298
- package/lib/index.js +8 -20
- package/package.json +36 -36
- package/src/View.js +3 -4
- package/src/index.js +3 -3
package/lib/Emitter.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
28
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
this.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
|
-
|
|
5
|
-
var _Emitter2 = require(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
function
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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;
|