Comparing version 4.0.2 to 5.0.0
# Changelog | ||
## `5.0.0` | ||
- Made `set` accept models too, not only attributes. | ||
- Now, `save` will add the model to the collection if one is assigned. This allows people to use `collection.build + save` effectively. | ||
## `4.0.2` | ||
@@ -4,0 +9,0 @@ |
@@ -139,3 +139,3 @@ import Base from './Base'; | ||
[key: string]: any; | ||
}>, { add, change, remove }?: SetOptions): void; | ||
} | T>, { add, change, remove }?: SetOptions): void; | ||
/** | ||
@@ -142,0 +142,0 @@ * Creates a new model instance with the given attributes |
@@ -19,2 +19,3 @@ 'use strict'; | ||
var entries = _interopDefault(require('lodash/entries')); | ||
var compact = _interopDefault(require('lodash/compact')); | ||
@@ -415,2 +416,3 @@ /*! ***************************************************************************** | ||
var label = this.isNew ? 'creating' : 'updating'; | ||
var collection = this.collection; | ||
var data; | ||
@@ -441,2 +443,5 @@ if (patch && attributes && !this.isNew) { | ||
} | ||
if (optimistic && collection) { | ||
collection.set([this], { remove: false }); | ||
} | ||
var onProgress = debounce(function (progress) { | ||
@@ -453,2 +458,5 @@ if (optimistic && _this.request) | ||
_this.commitChanges(); | ||
if (!optimistic && collection) { | ||
collection.set([_this], { remove: false }); | ||
} | ||
if (keepChanges) { | ||
@@ -461,2 +469,5 @@ _this.set(applyPatchChanges(data, changes)); | ||
_this.set(currentAttributes); | ||
if (optimistic && _this.isNew) { | ||
collection.remove(_this); | ||
} | ||
}); | ||
@@ -552,2 +563,12 @@ return this.withRequest(['saving', label], promise, abort); | ||
function getAttribute(resource, attribute) { | ||
if (resource instanceof Model) { | ||
return resource.has(attribute) | ||
? resource.get(attribute) | ||
: null; | ||
} | ||
else { | ||
return resource[attribute]; | ||
} | ||
} | ||
var Collection = /** @class */ (function (_super) { | ||
@@ -797,3 +818,5 @@ __extends(Collection, _super); | ||
if (remove) { | ||
var ids = resources.map(function (r) { return r[_this.primaryKey]; }); | ||
var ids = compact(resources.map(function (r) { | ||
return getAttribute(r, _this.primaryKey); | ||
})); | ||
var toRemove = difference(this._ids, ids); | ||
@@ -804,3 +827,4 @@ if (toRemove.length) | ||
resources.forEach(function (resource) { | ||
var model = _this.get(resource[_this.primaryKey]); | ||
var id = getAttribute(resource, _this.primaryKey); | ||
var model = id ? _this.get(id) : null; | ||
if (model && change) | ||
@@ -836,17 +860,10 @@ model.set(resource); | ||
var model = this.build(attributesOrModel); | ||
var request = model.save(); | ||
var request = model.save({}, { optimistic: optimistic }); | ||
this.requests.push(request); | ||
var promise = request.promise; | ||
if (optimistic) { | ||
this.add(model); | ||
} | ||
promise | ||
.then(function (response) { | ||
if (!optimistic) | ||
_this.add(model); | ||
.then(function (_response) { | ||
_this.requests.remove(request); | ||
}) | ||
.catch(function (error) { | ||
if (optimistic) | ||
_this.remove(model); | ||
_this.requests.remove(request); | ||
@@ -853,0 +870,0 @@ }); |
{ | ||
"name": "mobx-rest", | ||
"version": "4.0.2", | ||
"version": "5.0.0", | ||
"description": "REST conventions for mobx.", | ||
@@ -5,0 +5,0 @@ "jest": { |
@@ -269,2 +269,3 @@ # mobx-rest | ||
It tracks the state of the request using the label `saving`. | ||
If the model has a collection associated, it will be added into it. | ||
@@ -485,3 +486,3 @@ Options: | ||
#### `set(models: Array<Object>, options: Object): void` | ||
#### `set(models: Array<Object | Model>, options: Object): void` | ||
@@ -488,0 +489,0 @@ Merge the given models smartly the current ones in the collection. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
70606
1286
756