-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJSModel.js
More file actions
34 lines (29 loc) · 703 Bytes
/
JSModel.js
File metadata and controls
34 lines (29 loc) · 703 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// Copyright © 2017-Present, Gaurav D. Sharma
// All rights reserved.
//
'use strict';
export default class JSONModel {
constructor(json = {}) {
if (this.validate(json)) {
Object.assign(this, json);
this.loadExtra(json);
}
}
validate(json) {
return typeof json !== 'undefined';
}
/// this method use in case you want to hack(prototype) your model and want to introduced new keys
loadExtra(json = {}) {}
// id => appID, first_name => firstName
// pass args as `{id: 'appID', first_name: 'firstName'}`
keyMapper(hash) {
for (const key in hash) {
this[hash[key]] = this[key];
delete this[key];
}
}
clone() {
return Object.assign(Object.create(this), this);
}
}