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/es/Model.js CHANGED
@@ -1,154 +1,140 @@
1
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
-
3
- 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; }
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, and triggers "change" events
13
- * when any of its data is modified.<br />
14
- * Models may handle syncing data with a persistence layer.<br />
15
- * Design your models as the atomic reusable objects containing all of the helpful functions for
16
- * manipulating their particular bit of data.<br />
17
- * Models should be able to be passed around throughout your app, and used anywhere that bit of data is needed.<br />
18
- * Rasti Models stores its attributes in `this.attributes`, which is extended from `this.defaults` and constructor `attrs` parameter.
19
- * For every attribute, a getter is generated, which retrieve the model property from `this.attributes`.
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
- * // Todo model
25
- * class TodoModel extends Rasti.Model {
26
- * toggle() {
27
- * // Set completed property. This will call a setter that will set `completed`
28
- * // in this.attributes, and emit `change` and `change:completed` events.
29
- * this.completed = !this.completed;
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
- * // Todo model has `title` and `completed` default attributes. `defaults` will extend `this.attributes`. Getters and setters are generated for `this.attributtes`, in order to emit `change` events.
33
- * TodoModel.prototype.defaults = {
34
- * title : '',
35
- * completed : false
36
- * };
37
- * // Create todo. Pass `title` attribute as argument.
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
- var Model = function (_Emitter) {
46
- _inherits(Model, _Emitter);
47
-
48
- function Model() {
49
- var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
50
-
51
- _classCallCheck(this, Model);
52
-
53
- // Call preinitialize.
54
- var _this = _possibleConstructorReturn(this, _Emitter.call(this));
55
-
56
- _this.preinitialize.apply(_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(_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
-
71
-
72
- Model.prototype.preinitialize = function preinitialize() {};
73
-
74
- /**
75
- * Generate getter/setter for the given key. In order to emit `change` events.
76
- * This method is called internally by the constructor
77
- * for `this.attributes`.
78
- * @param {string} key Attribute key.
79
- */
80
-
81
-
82
- Model.prototype.defineAttribute = function defineAttribute(key) {
83
- var _this2 = this;
84
-
85
- Object.defineProperty(this, key, {
86
- get: function get() {
87
- return _this2.get(key);
88
- },
89
- set: function set(value) {
90
- _this2.set(key, value);
91
- }
92
- });
93
- };
94
-
95
- /**
96
- * Get an attribute from `this.attributes`.
97
- * This method is called internally by generated getters.
98
- * @param {string} key Attribute key.
99
- * @return {any} The attribute value.
100
- */
101
-
102
-
103
- Model.prototype.get = function get(key) {
104
- return this.attributes[key];
105
- };
106
-
107
- /**
108
- * Set an attribute into `this.attributes`.
109
- * Emit `change` and `change:attribute` if value change.
110
- * Could be called in two forms, `this.set('key', value)` and
111
- * `this.set({ key : value })`.
112
- * This method is called internally by generated setters.
113
- * @param {string} key Attribute key or object containing keys/values.
114
- * @param [value] Attribute value.
115
- * @return {this} This model.
116
- * @emits change
117
- * @emits change:attribute
118
- */
119
-
120
-
121
- Model.prototype.set = function set(key, value) {
122
- var _ref,
123
- _this3 = this;
124
-
125
- var attrs = typeof key === 'object' ? key : (_ref = {}, _ref[key] = value, _ref);
126
- Object.keys(attrs).forEach(function (key) {
127
- var changed = key in _this3.attributes && attrs[key] !== _this3.attributes[key];
128
-
129
- _this3.previous[key] = _this3.attributes[key];
130
- _this3.attributes[key] = attrs[key];
131
- // Emit change events.
132
- if (changed) {
133
- _this3.emit('change', _this3, key, attrs[key]);
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 };