dataflux 1.5.2 → 1.5.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 +43 -7
- package/dist/Obj.js +2 -2
- package/dist/SubObj.js +23 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -87,7 +87,7 @@ Nothing else to do! After your edit, the store will do a single PUT request to t
|
|
|
87
87
|
|
|
88
88
|
### Example 2
|
|
89
89
|
|
|
90
|
-
DataFlux automatically sends the edited objects back to the API to be saved. However, you can disable this behavior and manually instruct the store when to save.
|
|
90
|
+
DataFlux automatically sends the edited objects back to the API to be saved. However, you can disable this behavior and manually instruct the store when to save.
|
|
91
91
|
|
|
92
92
|
```js
|
|
93
93
|
// To disable autoSave you must declare the store (in store.js) as follows
|
|
@@ -100,7 +100,7 @@ The same example above now becomes:
|
|
|
100
100
|
// Find the author Dante Alighieri
|
|
101
101
|
store.find("author", ({name, surname}) => name == "Dante" && surname == "Alighieri")
|
|
102
102
|
.then(([author]) => {
|
|
103
|
-
|
|
103
|
+
|
|
104
104
|
// When autoSave is false, author.set("country", "Italy") and
|
|
105
105
|
// author.country = "Italy" are equivalent
|
|
106
106
|
author.country = "Italy"
|
|
@@ -174,12 +174,12 @@ You can also do multiple subscriptions at once:
|
|
|
174
174
|
|
|
175
175
|
```js
|
|
176
176
|
const subscriptions = [
|
|
177
|
-
|
|
178
|
-
|
|
177
|
+
["book", ({title}) => title === "The little prince"], // Model name and filter function
|
|
178
|
+
["author"], // No filter function, all objects returned
|
|
179
179
|
];
|
|
180
180
|
|
|
181
181
|
const callback = ({book, author}) => {
|
|
182
|
-
|
|
182
|
+
// Objects are ready
|
|
183
183
|
};
|
|
184
184
|
|
|
185
185
|
const subKey = store.multipleSubscribe(requests, callback); // Subscribe
|
|
@@ -221,7 +221,7 @@ class MyComponent extends React.Component {
|
|
|
221
221
|
onTitleChange={(title) => book.set("title", title)}
|
|
222
222
|
// onTitleChange will alter the book and so the current
|
|
223
223
|
// state of "books" (a setState will be performed).
|
|
224
|
-
|
|
224
|
+
|
|
225
225
|
// Alternatively:
|
|
226
226
|
// onTitleChange={store.handleChange(book, "title")}
|
|
227
227
|
// is a syntactic sugar of the function above
|
|
@@ -493,11 +493,47 @@ Each object created is enriched with the following methods.
|
|
|
493
493
|
| getRelation(model, filterFunction) | To get all the objects respecting a specific relation with this object (see [model relations](#model-relations)). |
|
|
494
494
|
| save() | Method to save the object. You can do `store.save()` instead. |
|
|
495
495
|
| destroy() | Method to delete the object. You can do `store.delete()` instead. |
|
|
496
|
-
|
|
497
496
|
| toJSON() | It returns a pure JSON representation of the object. |
|
|
498
497
|
| toString() | It returns a string representation of the object. |
|
|
499
498
|
| getFingerprint() | It returns a hash of the object. The hash changes at every change of the object or of any nested object. Useful to detect object changes. |
|
|
500
499
|
| getModel() | It returns the model of this object. Mostly useful to do `object.getModel().getType()` and obtain a string defining the type of the object. |
|
|
500
|
+
### Deeper Objects
|
|
501
|
+
When a model is declared with the option `deep: true` (default, see [model creation](#models-creation)), all the sub objects will also offer many of the methods above.
|
|
502
|
+
|
|
503
|
+
Imagine the API returns:
|
|
504
|
+
|
|
505
|
+
```json
|
|
506
|
+
[
|
|
507
|
+
{
|
|
508
|
+
"title": "The little prince",
|
|
509
|
+
"reviews": [
|
|
510
|
+
{
|
|
511
|
+
"stars": 4,
|
|
512
|
+
"comment": "comment 1"
|
|
513
|
+
},
|
|
514
|
+
{
|
|
515
|
+
"stars": 3,
|
|
516
|
+
"comment": "comment 2"
|
|
517
|
+
}
|
|
518
|
+
]
|
|
519
|
+
},
|
|
520
|
+
...
|
|
521
|
+
]
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
You can operate on the reviews similarly to how you operate on the main model's objects (book).
|
|
525
|
+
|
|
526
|
+
```js
|
|
527
|
+
store.find("book")
|
|
528
|
+
.then(([book]) => {
|
|
529
|
+
const firstReview = book.reviews[0];
|
|
530
|
+
|
|
531
|
+
// Examples of what you can do:
|
|
532
|
+
firstReview.detroy(); // The first review is removed from the array book.reviews
|
|
533
|
+
firstReview.set("stars", 5); // Set the stars of the first review to 5
|
|
534
|
+
});
|
|
535
|
+
```
|
|
536
|
+
|
|
501
537
|
|
|
502
538
|
## Editing objects
|
|
503
539
|
The option `autoSave` can be `true`, `false`, or a number (milliseconds).
|
package/dist/Obj.js
CHANGED
|
@@ -117,10 +117,10 @@ var Obj = /*#__PURE__*/function (_BasicObj) {
|
|
|
117
117
|
var mmnt = (0, _moment["default"])(value);
|
|
118
118
|
_this[key] = mmnt.isValid() ? mmnt : value;
|
|
119
119
|
} else if (_model.options.deep && _typeof(value) === "object" && !Array.isArray(value)) {
|
|
120
|
-
_this[key] = new _SubObj["default"](_assertThisInitialized(_this), value, _model);
|
|
120
|
+
_this[key] = new _SubObj["default"](_assertThisInitialized(_this), key, value, _model);
|
|
121
121
|
} else if (_model.options.deep && Array.isArray(value)) {
|
|
122
122
|
_this[key] = value.map(function (i) {
|
|
123
|
-
return new _SubObj["default"](_assertThisInitialized(_this), i, _model);
|
|
123
|
+
return new _SubObj["default"](_assertThisInitialized(_this), key, i, _model);
|
|
124
124
|
});
|
|
125
125
|
} else {
|
|
126
126
|
_this[key] = value;
|
package/dist/SubObj.js
CHANGED
|
@@ -5,8 +5,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports["default"] = void 0;
|
|
7
7
|
|
|
8
|
-
var _uuid = require("uuid");
|
|
9
|
-
|
|
10
8
|
var _BasicObj2 = require("./BasicObj");
|
|
11
9
|
|
|
12
10
|
var _moment = _interopRequireDefault(require("moment/moment"));
|
|
@@ -55,12 +53,14 @@ var _model = /*#__PURE__*/new WeakMap();
|
|
|
55
53
|
|
|
56
54
|
var _parent = /*#__PURE__*/new WeakMap();
|
|
57
55
|
|
|
56
|
+
var _parentField = /*#__PURE__*/new WeakMap();
|
|
57
|
+
|
|
58
58
|
var SubObj = /*#__PURE__*/function (_BasicObj) {
|
|
59
59
|
_inherits(SubObj, _BasicObj);
|
|
60
60
|
|
|
61
61
|
var _super = _createSuper(SubObj);
|
|
62
62
|
|
|
63
|
-
function SubObj(parent, values, model) {
|
|
63
|
+
function SubObj(parent, field, values, model) {
|
|
64
64
|
var _this;
|
|
65
65
|
|
|
66
66
|
_classCallCheck(this, SubObj);
|
|
@@ -77,12 +77,27 @@ var SubObj = /*#__PURE__*/function (_BasicObj) {
|
|
|
77
77
|
value: void 0
|
|
78
78
|
});
|
|
79
79
|
|
|
80
|
+
_classPrivateFieldInitSpec(_assertThisInitialized(_this), _parentField, {
|
|
81
|
+
writable: true,
|
|
82
|
+
value: void 0
|
|
83
|
+
});
|
|
84
|
+
|
|
80
85
|
_defineProperty(_assertThisInitialized(_this), "save", function () {
|
|
81
86
|
return _classPrivateFieldGet(_assertThisInitialized(_this), _model).getStore().save([_classPrivateFieldGet(_assertThisInitialized(_this), _parent)]);
|
|
82
87
|
});
|
|
83
88
|
|
|
84
89
|
_defineProperty(_assertThisInitialized(_this), "destroy", function () {
|
|
85
|
-
|
|
90
|
+
var _classPrivateFieldGet2;
|
|
91
|
+
|
|
92
|
+
if (Array.isArray(_classPrivateFieldGet(_assertThisInitialized(_this), _parent)[_classPrivateFieldGet(_assertThisInitialized(_this), _parentField)])) {
|
|
93
|
+
_classPrivateFieldGet(_assertThisInitialized(_this), _parent)[_classPrivateFieldGet(_assertThisInitialized(_this), _parentField)] = _classPrivateFieldGet(_assertThisInitialized(_this), _parent)[_classPrivateFieldGet(_assertThisInitialized(_this), _parentField)].filter(function (i) {
|
|
94
|
+
return i.getId() !== _this.getId();
|
|
95
|
+
});
|
|
96
|
+
} else if ((_classPrivateFieldGet2 = _classPrivateFieldGet(_assertThisInitialized(_this), _parent)[_classPrivateFieldGet(_assertThisInitialized(_this), _parentField)]) !== null && _classPrivateFieldGet2 !== void 0 && _classPrivateFieldGet2.getId) {
|
|
97
|
+
_classPrivateFieldGet(_assertThisInitialized(_this), _parent)[_classPrivateFieldGet(_assertThisInitialized(_this), _parentField)] = null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return _classPrivateFieldGet(_assertThisInitialized(_this), _model).getStore().update([_classPrivateFieldGet(_assertThisInitialized(_this), _parent)]);
|
|
86
101
|
});
|
|
87
102
|
|
|
88
103
|
_defineProperty(_assertThisInitialized(_this), "update", function () {
|
|
@@ -93,6 +108,8 @@ var SubObj = /*#__PURE__*/function (_BasicObj) {
|
|
|
93
108
|
|
|
94
109
|
_classPrivateFieldSet(_assertThisInitialized(_this), _parent, parent);
|
|
95
110
|
|
|
111
|
+
_classPrivateFieldSet(_assertThisInitialized(_this), _parentField, field);
|
|
112
|
+
|
|
96
113
|
Object.keys(values).forEach(function (key) {
|
|
97
114
|
var value = values[key];
|
|
98
115
|
|
|
@@ -100,10 +117,10 @@ var SubObj = /*#__PURE__*/function (_BasicObj) {
|
|
|
100
117
|
var mmnt = (0, _moment["default"])(value);
|
|
101
118
|
_this[key] = mmnt.isValid() ? mmnt : value;
|
|
102
119
|
} else if (model.options.deep && _typeof(value) === "object" && !Array.isArray(value)) {
|
|
103
|
-
_this[key] = new SubObj(_classPrivateFieldGet(_assertThisInitialized(_this), _parent), value, model);
|
|
120
|
+
_this[key] = new SubObj(_classPrivateFieldGet(_assertThisInitialized(_this), _parent), key, value, model);
|
|
104
121
|
} else if (model.options.deep && Array.isArray(value)) {
|
|
105
122
|
_this[key] = value.map(function (i) {
|
|
106
|
-
return new SubObj(_classPrivateFieldGet(_assertThisInitialized(_this), _parent), i, model);
|
|
123
|
+
return new SubObj(_classPrivateFieldGet(_assertThisInitialized(_this), _parent), key, i, model);
|
|
107
124
|
});
|
|
108
125
|
} else {
|
|
109
126
|
_this[key] = value;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dataflux",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.3",
|
|
4
4
|
"description": "DataFlux, automatically interfaces with your REST APIs to create a 2-way-synced local data store. Transparently manages data propagation in the React state.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"bin": "dist/index.js",
|