rasti 1.1.1 → 2.0.0-alpha.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 +21 -27
- package/dist/rasti.js +66 -116
- package/dist/rasti.min.js +1 -1
- package/es/Component.js +398 -0
- package/es/Emitter.js +113 -116
- package/es/Model.js +128 -142
- package/es/View.js +258 -327
- package/es/index.js +2 -2
- package/lib/Component.js +402 -0
- package/lib/Emitter.js +115 -118
- package/lib/Model.js +133 -150
- package/lib/View.js +263 -336
- package/lib/index.js +10 -20
- package/package.json +39 -37
- package/src/Component.js +397 -0
- package/src/Emitter.js +29 -11
- package/src/Model.js +34 -26
- package/src/View.js +25 -43
- package/src/index.js +2 -1
package/es/Model.js
CHANGED
|
@@ -1,154 +1,140 @@
|
|
|
1
|
-
function
|
|
2
|
-
|
|
3
|
-
function
|
|
4
|
-
|
|
5
|
-
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; }
|
|
6
|
-
|
|
1
|
+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
2
|
+
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
|
|
3
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
7
4
|
import Emitter from './Emitter';
|
|
8
5
|
/**
|
|
9
6
|
* - Orchestrates data and business logic.
|
|
10
7
|
* - Emits events when data changes.
|
|
11
8
|
*
|
|
12
|
-
* A Model manages an internal table of data attributes
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* And a setter, which sets the model property in `this.attributes` and emits `change` and `change:attribute` events.
|
|
9
|
+
* A `Model` manages an internal table of data attributes and triggers change events when any of its data is modified.<br />
|
|
10
|
+
* Models may handle syncing data with a persistence layer. To design your models, create atomic, reusable objects
|
|
11
|
+
* that contain all the necessary functions for manipulating their specific data.<br />
|
|
12
|
+
* Models should be easily passed throughout your app and used anywhere the corresponding data is needed.<br />
|
|
13
|
+
* Rasti models stores its attributes in `this.attributes`, which is extended from `this.defaults` and the
|
|
14
|
+
* constructor `attrs` parameter. For every attribute, a getter is generated to retrieve the model property
|
|
15
|
+
* from `this.attributes`, and a setter is created to set the model property in `this.attributes` and emit `change`
|
|
16
|
+
* and `change:attribute` events.
|
|
21
17
|
* @module
|
|
22
18
|
* @param {object} attrs Object containing model attributes to extend `this.attributes`. Getters and setters are generated for `this.attributtes`, in order to emit `change` events.
|
|
23
19
|
* @example
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* //
|
|
29
|
-
*
|
|
20
|
+
* import { Model } from 'rasti';
|
|
21
|
+
* // Product model
|
|
22
|
+
* class ProductModel extends Model {
|
|
23
|
+
* preinitialize() {
|
|
24
|
+
* // The Product model has `name` and `price` default attributes.
|
|
25
|
+
* // `defaults` will extend `this.attributes`.
|
|
26
|
+
* // Getters and setters are generated for `this.attributes`,
|
|
27
|
+
* // in order to emit `change` events.
|
|
28
|
+
* this.defaults = {
|
|
29
|
+
* name: '',
|
|
30
|
+
* price: 0
|
|
31
|
+
* };
|
|
32
|
+
* }
|
|
33
|
+
*
|
|
34
|
+
* setDiscount(discountPercentage) {
|
|
35
|
+
* // Apply a discount to the price property.
|
|
36
|
+
* // This will call a setter that will update `price` in `this.attributes`,
|
|
37
|
+
* // and emit `change` and `change:price` events.
|
|
38
|
+
* const discount = this.price * (discountPercentage / 100);
|
|
39
|
+
* this.price -= discount;
|
|
30
40
|
* }
|
|
31
41
|
* }
|
|
32
|
-
* //
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* const todo = new TodoModel({ title : 'Learn Rasti' });
|
|
39
|
-
* // Listen to `change:completed` event.
|
|
40
|
-
* todo.on('change:completed', () => console.log('Completed:', todo.completed));
|
|
41
|
-
* // Complete todo.
|
|
42
|
-
* todo.toggle(); // Completed: true
|
|
42
|
+
* // Create a product instance with a name and price.
|
|
43
|
+
* const product = new ProductModel({ name: 'Smartphone', price: 1000 });
|
|
44
|
+
* // Listen to the `change:price` event.
|
|
45
|
+
* product.on('change:price', () => console.log('New Price:', product.price));
|
|
46
|
+
* // Apply a 10% discount to the product.
|
|
47
|
+
* product.setDiscount(10); // Output: "New Price: 900"
|
|
43
48
|
*/
|
|
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
|
-
|
|
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
|
-
_this3.emit('change:' + key, _this3, attrs[key]);
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
return this;
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Return object representation of the model to be used for JSON serialization.
|
|
142
|
-
* By default returns `this.attributes`.
|
|
143
|
-
* @return {object} Object representation of the model to be used for JSON serialization.
|
|
144
|
-
*/
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
Model.prototype.toJSON = function toJSON() {
|
|
148
|
-
return this.attributes;
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
return Model;
|
|
49
|
+
var Model = /*#__PURE__*/function (_Emitter) {
|
|
50
|
+
_inheritsLoose(Model, _Emitter);
|
|
51
|
+
function Model() {
|
|
52
|
+
var _this;
|
|
53
|
+
var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
54
|
+
_this = _Emitter.call(this) || this;
|
|
55
|
+
// Call preinitialize.
|
|
56
|
+
_this.preinitialize.apply(_assertThisInitialized(_this), arguments);
|
|
57
|
+
// attributes object.
|
|
58
|
+
_this.attributes = Object.assign({}, _this.defaults || {}, attrs);
|
|
59
|
+
// Previous attributes.
|
|
60
|
+
_this.previous = {};
|
|
61
|
+
// Generate getters/setters for every attr.
|
|
62
|
+
Object.keys(_this.attributes).forEach(_this.defineAttribute.bind(_assertThisInitialized(_this)));
|
|
63
|
+
return _this;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 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.
|
|
68
|
+
* @param {object} attrs Object containing model attributes to extend `this.attributes`.
|
|
69
|
+
*/
|
|
70
|
+
var _proto = Model.prototype;
|
|
71
|
+
_proto.preinitialize = function preinitialize() {}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Generate getter/setter for the given key. In order to emit `change` events.
|
|
75
|
+
* This method is called internally by the constructor
|
|
76
|
+
* for `this.attributes`.
|
|
77
|
+
* @param {string} key Attribute key.
|
|
78
|
+
*/;
|
|
79
|
+
_proto.defineAttribute = function defineAttribute(key) {
|
|
80
|
+
var _this2 = this;
|
|
81
|
+
Object.defineProperty(this, key, {
|
|
82
|
+
get: function get() {
|
|
83
|
+
return _this2.get(key);
|
|
84
|
+
},
|
|
85
|
+
set: function set(value) {
|
|
86
|
+
_this2.set(key, value);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Get an attribute from `this.attributes`.
|
|
93
|
+
* This method is called internally by generated getters.
|
|
94
|
+
* @param {string} key Attribute key.
|
|
95
|
+
* @return {any} The attribute value.
|
|
96
|
+
*/;
|
|
97
|
+
_proto.get = function get(key) {
|
|
98
|
+
return this.attributes[key];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Set an attribute into `this.attributes`.
|
|
103
|
+
* Emit `change` and `change:attribute` if value change.
|
|
104
|
+
* Could be called in two forms, `this.set('key', value)` and
|
|
105
|
+
* `this.set({ key : value })`.
|
|
106
|
+
* This method is called internally by generated setters.
|
|
107
|
+
* @param {string} key Attribute key or object containing keys/values.
|
|
108
|
+
* @param [value] Attribute value.
|
|
109
|
+
* @return {this} This model.
|
|
110
|
+
* @emits change
|
|
111
|
+
* @emits change:attribute
|
|
112
|
+
*/;
|
|
113
|
+
_proto.set = function set(key, value) {
|
|
114
|
+
var _ref,
|
|
115
|
+
_this3 = this;
|
|
116
|
+
var attrs = typeof key === 'object' ? key : (_ref = {}, _ref[key] = value, _ref);
|
|
117
|
+
Object.keys(attrs).forEach(function (key) {
|
|
118
|
+
var changed = key in _this3.attributes && attrs[key] !== _this3.attributes[key];
|
|
119
|
+
_this3.previous[key] = _this3.attributes[key];
|
|
120
|
+
_this3.attributes[key] = attrs[key];
|
|
121
|
+
// Emit change events.
|
|
122
|
+
if (changed) {
|
|
123
|
+
_this3.emit('change', _this3, key, attrs[key]);
|
|
124
|
+
_this3.emit("change:" + key, _this3, attrs[key]);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Return object representation of the model to be used for JSON serialization.
|
|
132
|
+
* By default returns `this.attributes`.
|
|
133
|
+
* @return {object} Object representation of the model to be used for JSON serialization.
|
|
134
|
+
*/;
|
|
135
|
+
_proto.toJSON = function toJSON() {
|
|
136
|
+
return this.attributes;
|
|
137
|
+
};
|
|
138
|
+
return Model;
|
|
152
139
|
}(Emitter);
|
|
153
|
-
|
|
154
|
-
export default Model;
|
|
140
|
+
export { Model as default };
|