dataflux 1.17.9 → 1.18.1

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 CHANGED
@@ -642,6 +642,7 @@ The store has the following method.
642
642
  | hasChanged(type, object) | This method receives in input a model type and an object (optional). It returns a boolean, `true` if the object is dirty (it changed but it has not yet being persisted). If the object is not passed, the method checks if any of the objects of the specified model type has changed. |
643
643
  | didUpdate(context) | If you pass an object as a prop to a React component, the component will not refresh if the object changes. To address this, you can use this method inside the `componentDidUpdate` to check if any of the objects changed. See [example 6](#example-7---react-props-and-observability) |
644
644
  | getCollection(type) | This method receives in input a model type . It returns all the raw (JSON) objects available for that model. |
645
+ | getModel(type) | It returns the model object given a model type. | |
645
646
 
646
647
  ### Insert vs. Mock
647
648
 
@@ -677,6 +678,16 @@ The store emits the following events:
677
678
  | loading | The event is emitted while a new model is loaded. The value contains something like `{status: "start", model: "book"}` |
678
679
  | refreshing | The event is emitted while a model is refreshed. The value contains something like `{status: "start", model: "book"}` |
679
680
 
681
+ ## Model methods
682
+
683
+ The model is an object that represents the entire collection of a given model type. While its role is relatively minor from a developer's perspective, it provides the following convenient methods and properties:
684
+
685
+ | Method | Description |
686
+ |------------|-----------------------------------------------------------------------|
687
+ | getType() | It returns the model type (a string). |
688
+ | getStore() | It returns the store managing the model. |
689
+ | ready | A boolean, true if the initial retrieval of the collection succeeded. |
690
+
680
691
  ## Objects methods
681
692
  Each object created is enriched with the following methods.
682
693
 
@@ -693,7 +704,7 @@ Each object created is enriched with the following methods.
693
704
  | toJSON() | It returns a pure JSON representation of the object. |
694
705
  | toString() | It returns a string representation of the object. |
695
706
  | 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. |
696
- | 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. |
707
+ | 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, or `object.getModel().ready` to check if the initial retrieve operation has been completed. |
697
708
  | getError() | If an operation on an object triggers an error, this error can be retrieved with `getError()`. This allows to observe specific objects' errors, instead of the generic `store.on("error", ...)`. |
698
709
  | getError(attributeName) | This method allows you to check if the specificed attribute generated any error according to the validation property specified in the model. See [objects validation](#objects-validation). |
699
710
  | setError(error) | Additionally to DataFlux's errors, you can trigger your own errors with this method. Other components observing this objet's error will be notified. |
package/dist/Model.js CHANGED
@@ -113,6 +113,7 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
113
113
  _classPrivateFieldInitSpec(this, _axios, void 0);
114
114
  _classPrivateFieldInitSpec(this, _loadFunction, void 0);
115
115
  _classPrivateFieldInitSpec(this, _hiddenFields, void 0);
116
+ _defineProperty(this, "ready", void 0);
116
117
  _defineProperty(this, "validateObjectAttribute", function (object, key) {
117
118
  var validate = _this.options.validate;
118
119
  if (validate && validate[key]) {
@@ -224,6 +225,7 @@ var Model = exports["default"] = /*#__PURE__*/_createClass(function Model(name)
224
225
  if (!Array.isArray(data)) {
225
226
  _classPrivateFieldSet(_singleItemQuery, _this, true);
226
227
  }
228
+ _this.ready = true;
227
229
  return _classPrivateFieldGet(_toArray, _this).call(_this, data);
228
230
  }).then(function (data) {
229
231
  var _this$options;
@@ -189,13 +189,18 @@ var ObserverStore = /*#__PURE__*/function (_PersistentStore) {
189
189
  var objects = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
190
190
  return (_classPrivateFieldGet(_unsubPromises, _this).length ? Promise.all(_classPrivateFieldGet(_unsubPromises, _this)) : Promise.resolve()).then(function () {
191
191
  if (objects.length) {
192
- var type = objects[0].getModel().getType();
193
- var uniqueSubs = _classPrivateFieldGet(_getUniqueSubs, _this).call(_this, objects, type);
194
- (0, _batchPromises["default"])(10, uniqueSubs, function (_ref3) {
195
- var callback = _ref3.callback,
196
- filterFunction = _ref3.filterFunction;
197
- return _this.find(type, filterFunction).then(callback);
198
- });
192
+ var _objects$;
193
+ var type = objects === null || objects === void 0 || (_objects$ = objects[0]) === null || _objects$ === void 0 || (_objects$ = _objects$.getModel()) === null || _objects$ === void 0 ? void 0 : _objects$.getType();
194
+ if (type) {
195
+ var uniqueSubs = _classPrivateFieldGet(_getUniqueSubs, _this).call(_this, objects, type);
196
+ (0, _batchPromises["default"])(10, uniqueSubs, function (_ref3) {
197
+ var callback = _ref3.callback,
198
+ filterFunction = _ref3.filterFunction;
199
+ return _this.find(type, filterFunction).then(callback);
200
+ });
201
+ } else {
202
+ console.log("Malformed update list", objects);
203
+ }
199
204
  }
200
205
  return objects;
201
206
  });
package/dist/Store.js CHANGED
@@ -66,6 +66,10 @@ var Store = exports["default"] = /*#__PURE__*/function () {
66
66
  _defineProperty(this, "getModels", function () {
67
67
  return Object.keys(_this.models);
68
68
  });
69
+ _defineProperty(this, "getModel", function (type) {
70
+ var _this$models$type;
71
+ return (_this$models$type = _this.models[type]) === null || _this$models$type === void 0 ? void 0 : _this$models$type.model;
72
+ });
69
73
  _defineProperty(this, "findSync", function (type, filterFunction) {
70
74
  var all = Object.values(_this.models[type].storedObjects).filter(function (i) {
71
75
  return i.status !== "deleted";