| 1 |
export default class BaseModel { |
| 2 |
/** |
| 3 |
* Clone to object to avoid changing the reference. |
| 4 |
* |
| 5 |
* @return {BaseModel} cloned model |
| 6 |
*/ |
| 7 |
clone() { |
| 8 |
const instance = new this.constructor(); |
| 9 |
|
| 10 |
Object.keys( this ).forEach( ( key ) => { |
| 11 |
instance[ key ] = this[ key ]; |
| 12 |
} ); |
| 13 |
|
| 14 |
return instance; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Using init and not the default constructor because there is a problem to fill the instance |
| 19 |
* dynamically in the constructor. |
| 20 |
* |
| 21 |
* @param {*} data |
| 22 |
* @return {BaseModel} model |
| 23 |
*/ |
| 24 |
init( data = {} ) { |
| 25 |
Object.entries( data ).forEach( ( [ key, value ] ) => { |
| 26 |
this[ key ] = value; |
| 27 |
} ); |
| 28 |
|
| 29 |
return this; |
| 30 |
} |
| 31 |
} |
| 32 |
|