Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding store save #134

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 128 additions & 51 deletions dist/mobx-spine.cjs.js

Large diffs are not rendered by default.

179 changes: 128 additions & 51 deletions dist/mobx-spine.es.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
],
"testPathIgnorePatterns": [
"/fixtures/"
]
],
"testURL": "https://localhost"
}
}
76 changes: 39 additions & 37 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,14 +764,7 @@ export default class Model {
return Promise.resolve(res);
});
}))
.catch(
action(err => {
if (err.valErrors) {
this.parseValidationErrors(err.valErrors);
}
throw err;
})
)
.catch(err => this._catchSave(err))
);
}

Expand All @@ -791,39 +784,48 @@ export default class Model {
}),
requestOptions: omit(options, 'relations', 'data', 'mapData'),
})
.then(action(res => {
this.saveFromBackend(res);
this.clearUserFieldChanges();
.then(res => this._afterSaveAll(res, options))
.catch(err => this._catchSave(err))
);
}

forNestedRelations(this, relationsToNestedKeys(options.relations || []), relation => {
if (relation instanceof Model) {
relation.clearUserFieldChanges();
} else {
relation.clearSetChanges();
}
});
@action
_afterSaveAll(res, options) {
this.saveFromBackend(res);
this.clearUserFieldChanges();

return this.saveAllFiles(relationsToNestedKeys(options.relations || [])).then(() => {
this.clearUserFileChanges();
forNestedRelations(this, relationsToNestedKeys(options.relations || []), relation => {
if (relation instanceof Model) {
relation.clearUserFieldChanges();
} else {
relation.clearSetChanges();
}
});

forNestedRelations(this, relationsToNestedKeys(options.relations || []), relation => {
if (relation instanceof Model) {
relation.clearUserFileChanges();
}
});
return this.saveAllFiles(relationsToNestedKeys(options.relations || []))
.then(() => this._afterSaveAllFiles(options))
.then(() => res);
}

return res;
});
}))
.catch(
action(err => {
if (err.valErrors) {
this.parseValidationErrors(err.valErrors);
}
throw err;
})
)
);
@action
_afterSaveAllFiles(options) {
this.clearUserFileChanges();

forNestedRelations(this, relationsToNestedKeys(options.relations || []), relation => {
if (relation instanceof Model) {
relation.clearUserFileChanges();
}
});

return;
}

@action
_catchSave(err) {
if (err.valErrors) {
this.parseValidationErrors(err.valErrors);
}
throw err;
}

// After saving a model, we should get back an ID mapping from the backend which looks like:
Expand Down
41 changes: 40 additions & 1 deletion src/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
result,
uniqBy,
} from 'lodash';
import { invariant } from './utils';
import { invariant, relationsToNestedKeys } from './utils';

const AVAILABLE_CONST_OPTIONS = [
'relations',
'limit',
Expand Down Expand Up @@ -500,4 +501,42 @@ export default class Store {
}
return Promise.all(promises);
}

save(options = {}) {
return this._saveAll(options);
}

@action
_saveAll(options = {}) {
this.clearValidationErrors();
return this.wrapPendingRequestCount(
this.__getApi()
.saveAllModels({
url: result(this, 'url'),
model: this,
data: this.toBackendAll({
data: options.data,
mapData: options.mapData,
nestedRelations: relationsToNestedKeys(options.relations || []),
onlyChanges: options.onlyChanges,
}),
requestOptions: omit(options, 'relations', 'data', 'mapData'),
})
.then(action(res => {
const promises = [];
for (const model of this.models) {
promises.push(model._afterSaveAll(res, options));
}
return Promise.all(promises);
Comment on lines +526 to +530
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should also call this.clearSetChanges after successful save, otherwise this.hasUserChanges will always stay true even after saving the store. I encountered this issue in a project that already uses changes from this PR

}))
.catch(
action(err => {
if (err.valErrors) {
this.parseValidationErrors(err.valErrors);
}
throw err;
})
)
);
}
}
Loading