@scripty/react-store
Version:
This lightweight global react hook store is inspired by the extjs store architecture. Share your stores through your application with only a few lines of code!
43 lines (36 loc) • 863 B
JavaScript
export class Model {
constructor(model, callback) {
this.callback = callback;
model.fields.forEach((rec) => {
this[rec.name] = (rec.default) ? rec.default : this.getInitialValue(rec.type)
})
}
getInitialValue (type) {
switch (type) {
case 'string':
return '';
case 'object':
return {};
case 'array':
return [];
case 'number':
return 0;
default:
return ''
}
}
set(record) {
Object.keys(record).forEach((key) => {
this[key] = record[key];
});
if (this.callback) {
this.callback();
}
}
setDirty() {
this.dirty = true;
}
get(field) {
return this[field]
}
}