forked from nickforddev/vue-models
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.js
171 lines (161 loc) · 4.13 KB
/
model.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {
path,
mergeDeepRight,
isEmpty
} from 'ramda'
import { ISODate } from './demo/types'
import * as utils from './utils'
let Vue
class Model {
static init (_Vue, _options) {
utils.init(_options)
Vue = process.env.NODE_ENV === 'test'
? require('vue')
: _Vue
}
static schema() {
return {
id: {
type: String
},
created: {
type: ISODate
},
updated: {
type: ISODate
}
}
}
static defaults() {
return {}
}
constructor(attributes = {}, options = {}) {
const schema = this.constructor.schema()
const default_attributes = utils.getDefaultsFromSchema(schema)
const _options = mergeDeepRight(this.constructor.defaults(), options)
const default_options = {
name: 'model',
created() {
this.set(attributes)
},
data() {
return default_attributes()
},
computed: {
basePath() {
const basePath = this.$options.basePath
return basePath || this.$options.name + 's'
},
urlRoot() {
const url = _options.url
? typeof _options.url === 'function'
? _options.url()
: _options.url
: `${this.basePath}/${this.id}`
return url
},
isNew() {
return [undefined, ''].includes(this.id)
},
url() {
const url = _options.url
? typeof _options.url === 'function'
? _options.url()
: _options.url
: this.isNew
? this.basePath
: this.urlRoot
return url
},
$request() {
return path(['$request'], this._vm) || utils.Request
}
},
methods: {
async fetch() {
let response = await this.$request(this.urlRoot)
this.set(response)
return response
},
destroy() {
return this.$request(this.urlRoot, {
method: 'delete'
})
},
save(_body, options = {}) {
let _options = {
path: '',
diff: true,
consume: true,
method: 'put'
}
_options = mergeDeepRight(_options, options)
const changed = !options.diff || this.isNew
? _body
: utils.getDiff(this.$data, _body)
if (isEmpty(changed)) {
return Promise.resolve()
}
const body = this.encode(changed)
let method
if (options.method) {
method = options.method
} else if (this.isNew) {
method = 'post'
} else {
method = _options.method
}
let path = _options.path
? '/' + _options.path
: ''
if (_options.query) {
path += `?${utils.makeQueryString(_options.query)}`
}
const request = this.$request(this.url + path, {
method,
body
})
request.then((response) => {
const data = _options.consume
? response
: body
this.set(data)
})
return request
},
data() {
let data = {}
for (let key in schema) {
data[key] = this[key]
}
return mergeDeepRight({}, data)
},
set(data) {
const data_decoded = this.decode(data)
for (let key in data_decoded) {
this[key] = data_decoded[key]
}
return this
},
reset() {
return utils.resetState(this, default_attributes())
},
toJSON() {
return utils.modelToJSON(this)
},
decode(data = this.data()) {
return utils.decodeData(utils.removeUnderscores(data), schema)
},
encode(data = this.data()) {
return utils.addUnderscores(utils.encodeData(data, schema))
},
schema() {
return schema
}
}
}
const model_options = mergeDeepRight(default_options, _options)
return new Vue(model_options)
}
}
export { Model }