Skip to content

Commit b650de4

Browse files
committed
fixed nested schema definitions
1 parent 4c90583 commit b650de4

File tree

7 files changed

+128
-65
lines changed

7 files changed

+128
-65
lines changed

Diff for: index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<title>vue-request</title>
5+
<title>vue-models</title>
66
</head>
77
<body>
88
<div id="app"></div>

Diff for: src/demo/App.vue

+69-22
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,80 @@
11
<template>
22
<div id="app">
3-
<button @click="test('get')">Get</button>
4-
<button @click="test('put')">Put</button>
5-
<button @click="test('post')">Post</button>
6-
<button @click="test('delete')">Delete</button>
7-
8-
<div>
9-
<button @click="jsfiddle">Get JSFiddle</button>
10-
</div>
3+
<h1>vue-models</h1>
4+
<button @click="setUser">Set User</button>
5+
<button @click="resetUser">Reset User</button>
6+
<h2>decoded model:</h2>
7+
<pre>{{ $user }}</pre>
8+
<h2>encoded model:</h2>
9+
<pre>{{ encoded }}</pre>
1110
</div>
1211
</template>
1312

1413
<script>
14+
import User from './user'
1515
export default {
1616
name: 'app',
17+
models: {
18+
user() {
19+
return new User()
20+
}
21+
},
22+
computed: {
23+
encoded() {
24+
return this.$user.encode()
25+
}
26+
},
1727
methods: {
18-
test(method) {
19-
this.$request('/thing', {
20-
method
21-
})
22-
.then(response => {
23-
console.log({response})
24-
})
25-
.catch(err => {
26-
console.warn(err)
27-
})
28+
setUser() {
29+
this.$user = {
30+
id: {
31+
$oid: '586e6d75b7a7bc5c852c60a5'
32+
},
33+
created: {
34+
$date: '2016-12-16T00:00:00'
35+
},
36+
updated: '2016-12-16T00:00:00',
37+
role: 'admin',
38+
first_name: 'Tony',
39+
last_name: 'Tiger',
40+
41+
notifications: {
42+
alarm: {
43+
$date: '2016-12-16T00:00:00'
44+
},
45+
test: {
46+
one: {
47+
$date: '2016-12-16T00:00:00'
48+
},
49+
two: {
50+
three: {
51+
$date: '2016-12-16T00:00:00'
52+
}
53+
}
54+
}
55+
},
56+
things: [
57+
{
58+
id: {
59+
$oid: '42356d75b7a7bc5c52c11a90'
60+
},
61+
created: {
62+
$date: '2016-12-16T00:00:00'
63+
}
64+
},
65+
{
66+
id: {
67+
$oid: '09a11c25c5cb7a7b57d65324"'
68+
},
69+
created: {
70+
$date: '2016-12-16T00:00:00'
71+
}
72+
}
73+
]
74+
}
2875
},
29-
async jsfiddle() {
30-
const response = await this.$request('http://google.com/')
31-
console.log({response})
76+
resetUser() {
77+
this.$user.reset()
3278
}
3379
}
3480
}
@@ -41,6 +87,7 @@ export default {
4187
-moz-osx-font-smoothing: grayscale;
4288
text-align: center;
4389
color: #2c3e50;
44-
margin-top: 60px;
90+
text-align: left;
91+
padding: 30px;
4592
}
4693
</style>

Diff for: src/demo/index.js

+2-18
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
import Vue from 'vue'
22
import App from './App'
3-
import VueRequest from '../index'
43

4+
import VueModels from '../index'
55
Vue.config.productionTip = false
66

7-
const token = 'test'
8-
9-
Vue.use(VueRequest, {
10-
root: 'http://localhost:8080',
11-
headers: {
12-
Access() {
13-
return token
14-
},
15-
Refresh: 'test_refresh_token'
16-
},
17-
before() {
18-
console.log('fire this before')
19-
},
20-
timeout() {
21-
console.log('fire this on timeout')
22-
}
23-
})
7+
Vue.use(VueModels)
248

259
/* eslint-disable no-new */
2610
new Vue({

Diff for: src/demo/user.js

+45-20
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
import _ from 'lodash'
1+
// import _ from 'lodash'
22
import { Model } from '../index'
33
import { ObjectId, ISODate } from './types'
44

55
const defaults = {
66
name: 'user',
77
computed: {
88
basePath() {
9-
const basePath = this.$options.basePath
10-
return basePath || this.role + 's'
9+
if (this.role) {
10+
const basePath = this.$options.basePath
11+
return basePath || this.role + 's'
12+
} else {
13+
return ''
14+
}
1115
},
1216
full_name() {
13-
return `${this.first_name} ${this.last_name}`
17+
return this.role
18+
? `${this.first_name} ${this.last_name}`
19+
: ``
1420
},
1521
initials() {
16-
return `${this.first_name[0]}${this.last_name[0]}`.toUpperCase()
22+
return this.role
23+
? `${this.first_name[0]}${this.last_name[0]}`.toUpperCase()
24+
: ``
1725
}
1826
}
1927
}
@@ -45,26 +53,43 @@ export default class User extends Model {
4553
role: {
4654
type: String
4755
},
48-
password: {
49-
type: String
50-
},
51-
avatar_color: {
52-
type: String
53-
},
54-
dwolla: {
55-
type: Object
56-
},
5756
notifications: {
58-
type: Object
57+
type: Object,
58+
properties: {
59+
alarm: {
60+
type: ISODate
61+
},
62+
test: {
63+
type: Object,
64+
properties: {
65+
one: {
66+
type: ISODate
67+
},
68+
two: {
69+
type: Object,
70+
properties: {
71+
three: {
72+
type: ISODate
73+
}
74+
}
75+
}
76+
}
77+
}
78+
}
5979
},
6080
status: {
6181
type: Object
6282
},
63-
leases: {
64-
type: Array
65-
},
66-
terms_accepted: {
67-
type: Object
83+
things: {
84+
type: Array,
85+
items: {
86+
id: {
87+
type: ObjectId
88+
},
89+
created: {
90+
type: ISODate
91+
}
92+
}
6893
}
6994
}
7095
}

Diff for: src/mixin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const init = (vm) => {
2626
}
2727
if (isDef(vm.$models)) {
2828
if (!vm.$options.computed) {
29-
vm.$options.computed = {};
29+
vm.$options.computed = {}
3030
}
3131
for (let key in vm.$models) {
3232
makeComputedProp(vm, key)

Diff for: src/model.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default class Model {
3434
return basePath || this.$options.name + 's'
3535
},
3636
urlRoot() {
37-
return `${this.basePath}/${this.id}`;
37+
return `${this.basePath}/${this.id}`
3838
},
3939
isNew() {
4040
return this.id === undefined
@@ -98,6 +98,7 @@ export default class Model {
9898
return req
9999
},
100100
set(data) {
101+
console.log('set', data)
101102
const data_decoded = this.decode(data)
102103
_.merge(this, data_decoded)
103104
return this

Diff for: src/utils/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,21 @@ const traverse = (data, schema, func) => {
103103
output = data.map(item => {
104104
for (let key in item) {
105105
if (schema.items && key in schema.items) {
106-
item[key] = traverse(item[key], schema[key], func)
106+
if ([Object, Array, Function].includes(schema.items[key])) {
107+
item[key] = traverse(item[key], schema[key], func)
108+
} else {
109+
item[key] = func(item[key], schema.items[key])
110+
}
107111
}
108112
}
109113
return item
110114
})
111115
} else if (data instanceof Object) {
112116
for (let key in data) {
113-
if (schema.properties && key in schema.properties) {
117+
if (schema[key].items) {
114118
output[key] = traverse(data[key], schema[key], func)
119+
} else if (schema[key] && schema[key].properties) {
120+
output[key] = traverse(data[key], schema[key].properties, func)
115121
} else if (key in schema) {
116122
output[key] = func(data[key], schema[key])
117123
} else {

0 commit comments

Comments
 (0)