Skip to content

Commit 916675f

Browse files
author
Guillaume Chau
committed
fix: improve data handling fix #274
1 parent 69d40f4 commit 916675f

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

src/dollar-apollo.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ export class DollarApollo {
7777
}
7878

7979
get loading () {
80-
return this.vm.$data.$apolloData && this.vm.$data.$apolloData.loading !== 0
80+
return this.vm.$data.$apolloData.loading !== 0
81+
}
82+
83+
get data () {
84+
return this.vm.$data.$apolloData.data
8185
}
8286

8387
addSmartQuery (key, options) {

src/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const keywords = [
1010
'$subscribe',
1111
]
1212

13+
function hasProperty (holder, key) {
14+
return typeof holder !== 'undefined' && holder.hasOwnProperty(key)
15+
}
16+
1317
const launch = function launch () {
1418
const apolloProvider = this.$apolloProvider
1519

@@ -37,15 +41,17 @@ const launch = function launch () {
3741
defineReactiveSetter(this.$apollo, 'error', apollo.$error)
3842
defineReactiveSetter(this.$apollo, 'watchLoading', apollo.$watchLoading)
3943

44+
// Apollo Data
45+
Object.defineProperty(this, '$apolloData', {
46+
get: () => this.$data.$apolloData,
47+
enumerable: true,
48+
configurable: true,
49+
})
50+
4051
// watchQuery
4152
for (let key in apollo) {
4253
if (key.charAt(0) !== '$') {
43-
let propHasKeyProperty = false
44-
if (typeof this.$props !== 'undefined') {
45-
propHasKeyProperty = this.$props.hasOwnProperty(key)
46-
}
47-
48-
if (!this.hasOwnProperty(key) && !propHasKeyProperty && !this.$data.hasOwnProperty(key)) {
54+
if (!hasProperty(this, key) && !hasProperty(this.$props, key) && !hasProperty(this.$data, key)) {
4955
Object.defineProperty(this, key, {
5056
get: () => this.$data.$apolloData.data[key],
5157
enumerable: true,

src/smart-query.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ export default class SmartQuery extends SmartApollo {
2323
}
2424

2525
super(vm, key, options, autostart)
26+
27+
this.hasDataField = this.vm.$data.hasOwnProperty(key)
28+
if (this.hasDataField) {
29+
Object.defineProperty(this.vm.$data.$apolloData.data, key, {
30+
get: () => this.vm.$data[key],
31+
enumerable: true,
32+
configurable: true,
33+
})
34+
} else {
35+
Object.defineProperty(this.vm.$data, key, {
36+
get: () => this.vm.$data.$apolloData.data[key],
37+
enumerable: true,
38+
configurable: true,
39+
})
40+
}
2641
}
2742

2843
get client () {
@@ -105,11 +120,11 @@ export default class SmartQuery extends SmartApollo {
105120
// No result
106121
} else if (!this.options.manual) {
107122
if (typeof this.options.update === 'function') {
108-
this.vm.$set(this.vm.$data.$apolloData.data, this.key, this.options.update.call(this.vm, data))
123+
this.setData(this.options.update.call(this.vm, data))
109124
} else if (data[this.key] === undefined) {
110125
console.error(`Missing ${this.key} attribute on result`, data)
111126
} else {
112-
this.vm.$set(this.vm.$data.$apolloData.data, this.key, data[this.key])
127+
this.setData(data[this.key])
113128
}
114129
} else if (!hasResultCallback) {
115130
console.error(`${this.key} query must have a 'result' hook in manual mode`)
@@ -120,6 +135,10 @@ export default class SmartQuery extends SmartApollo {
120135
}
121136
}
122137

138+
setData (value) {
139+
this.vm.$set(this.hasDataField ? this.vm.$data : this.vm.$data.$apolloData.data, this.key, value)
140+
}
141+
123142
catchError (error) {
124143
super.catchError(error)
125144
this.loadingDone()

0 commit comments

Comments
 (0)