Added event emitters and fixed document deletion

This commit is contained in:
Jori Lallo
2017-08-03 16:48:07 +03:00
parent 0851cf3fc2
commit ad44c67a46
9 changed files with 65 additions and 12 deletions

View File

@ -8,11 +8,12 @@ import ErrorsStore from 'stores/ErrorsStore';
import parseTitle from '../../shared/parseTitle';
import type { User } from 'types';
import BaseModel from './BaseModel';
import Collection from './Collection';
const DEFAULT_TITLE = 'Untitled document';
class Document {
class Document extends BaseModel {
isSaving: boolean = false;
hasPendingChanges: boolean = false;
errors: ErrorsStore;
@ -109,14 +110,6 @@ class Document {
}
};
@action delete = async () => {
try {
await client.post('/documents.delete', { id: this.id });
} catch (e) {
this.errors.add('Document failed to delete');
}
};
@action fetch = async () => {
try {
const res = await client.post('/documents.info', { id: this.id });
@ -177,6 +170,19 @@ class Document {
return this;
};
@action delete = async () => {
try {
await client.post('/documents.delete', { id: this.id });
this.emit('document.delete', {
id: this.id,
collectionId: this.collection.id,
});
} catch (e) {
this.errors.add('Error while deleting the document');
}
return;
};
updateData(data: Object = {}, dirty: boolean = false) {
if (data.text) {
const { title, emoji } = parseTitle(data.text);
@ -189,6 +195,8 @@ class Document {
}
constructor(data?: Object = {}) {
super();
this.updateData(data);
this.errors = stores.errors;
}