- save & load

- key navigation
- ui improvements
This commit is contained in:
bkfox
2022-12-11 00:29:53 +01:00
parent cfc0e45439
commit 61af53eecb
10 changed files with 250 additions and 95 deletions

View File

@ -41,11 +41,15 @@ export default class Model {
this.commit(data);
}
get errors() {
return this.data.__errors__
}
/**
* Get instance id from its data
*/
static getId(data) {
return data.id;
return 'id' in data ? data.id : data.pk;
}
/**
@ -112,8 +116,16 @@ export default class Model {
* Update instance's data with provided data. Return None
*/
commit(data) {
this.id = this.constructor.getId(data);
this.data = data;
this.id = this.constructor.getId(this.data);
}
/**
* Update model data, without reset previous value
*/
update(data) {
this.data = {...this.data, ...data}
this.id = this.constructor.getId(this.data)
}
/**
@ -130,6 +142,13 @@ export default class Model {
let item = window.localStorage.getItem(key);
return item === null ? item : new this(JSON.parse(item));
}
/**
* Return error for a specific attribute name if any
*/
error(attr=null) {
return attr === null ? this.errors : this.errors && this.errors[attr]
}
}