Resolved deprecated usage of Ember Data evented api usage

no issue

- https://deprecations.emberjs.com/ember-data/v3.x/#toc_evented-api-usage
- we were using the now deprecated `didCreate` and `didUpdate` events on the post model to perform controller-specific logic each time `post.save()` was called
- moved the functionality that was attached to the hook events into a `_savePost()` function and used that anywhere we were calling `post.save()`
This commit is contained in:
Kevin Ansfield 2019-08-26 15:08:49 +01:00
parent 341d007acd
commit 2500cd9064

View File

@ -342,14 +342,14 @@ export default Controller.extend({
}
try {
let post = yield this.post.save(options);
let post = yield this._savePost.perform(options);
post.set('statusScratch', null);
if (!options.silent) {
this._showSaveNotification(prevStatus, post.get('status'), isNew ? true : false);
}
this.post.set('statusScratch', null);
// redirect to edit route if saving a new record
if (isNew && post.get('id')) {
if (!this.leaveEditorTransition) {
@ -432,14 +432,14 @@ export default Controller.extend({
return;
}
return yield this.post.save();
return yield this._savePost.perform();
}).group('saveTasks'),
// used in the PSM so that saves are sequential and don't trigger collision
// detection errors
savePost: task(function* () {
try {
return yield this.post.save();
return yield this._savePost.perform();
} catch (error) {
if (error) {
let status = this.get('post.status');
@ -450,6 +450,32 @@ export default Controller.extend({
}
}).group('saveTasks'),
// convenience method for saving the post and performing post-save cleanup
_savePost: task(function* (options) {
let {post} = this;
yield post.save(options);
// remove any unsaved tags
// NOTE: `updateTags` changes `hasDirtyAttributes => true`.
// For a saved post it would otherwise be false.
post.updateTags();
this._previousTagNames = this._tagNames;
// if the two "scratch" properties (title and content) match the post,
// then it's ok to set hasDirtyAttributes to false
// TODO: why is this necessary?
let titlesMatch = post.get('titleScratch') === post.get('title');
let bodiesMatch = JSON.stringify(post.get('scratch')) === JSON.stringify(post.get('mobiledoc'));
if (titlesMatch && bodiesMatch) {
this.set('hasDirtyAttributes', false);
}
return post;
}),
saveTitle: task(function* () {
let post = this.post;
let currentTitle = post.get('title');
@ -525,7 +551,6 @@ export default Controller.extend({
post.set('scratch', post.get('mobiledoc'));
this._previousTagNames = this._tagNames;
this._attachModelHooks();
// triggered any time the admin tab is closed, we need to use a native
// dialog here instead of our custom modal
@ -607,9 +632,6 @@ export default Controller.extend({
} else {
post.rollbackAttributes();
}
// remove the create/update event handlers that were added to the post
this._detachModelHooks();
}
this._previousTagNames = [];
@ -708,46 +730,6 @@ export default Controller.extend({
return post.get('hasDirtyAttributes');
},
// post.save() is called in multiple places, rather than remembering to
// add a .then in every instance we use model hooks to update our local
// values used for `hasDirtyAttributes`
_attachModelHooks() {
let post = this.post;
if (post) {
post.on('didCreate', this, this._postSaved);
post.on('didUpdate', this, this._postSaved);
}
},
_detachModelHooks() {
let post = this.post;
if (post) {
post.off('didCreate', this, this._postSaved);
post.off('didUpdate', this, this._postSaved);
}
},
_postSaved() {
let post = this.post;
// remove any unsaved tags
// NOTE: `updateTags` changes `hasDirtyAttributes => true`.
// For a saved post it would otherwise be false.
post.updateTags();
this._previousTagNames = this._tagNames;
// if the two "scratch" properties (title and content) match the post,
// then it's ok to set hasDirtyAttributes to false
// TODO: why is this necessary?
let titlesMatch = post.get('titleScratch') === post.get('title');
let bodiesMatch = JSON.stringify(post.get('scratch')) === JSON.stringify(post.get('mobiledoc'));
if (titlesMatch && bodiesMatch) {
this.set('hasDirtyAttributes', false);
}
},
_showSaveNotification(prevStatus, status, delay) {
let message = messageMap.success.post[prevStatus][status];
let notifications = this.notifications;