Skip to content

Commit 3918a77

Browse files
author
egoist
committed
release 0.0.1
1 parent 67539a8 commit 3918a77

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { directive as preload } from 'vue-preload'
1919
Vue.directive('preload', preload)
2020
```
2121

22-
Then replace your `v-on:click="handleClick"` with `v-preload:"handleClick"`, and do make a small changes:
22+
Then replace your `v-on:click="handleClick"` with `v-preload:"handleClick"`, and make a small change:
2323

2424
```javascript
2525
...

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-preload",
3-
"version": "0.0.0",
3+
"version": "0.0.1",
44
"description": "Preloading data for Vue component",
55
"main": "index.js",
66
"scripts": {

src/directive.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,50 @@ export const directive = {
44
bind() {
55
this.preLoading = false
66
this.tmp = {}
7+
this.firstBlood = false
8+
this.mouseOver = false
79
this.handleMouseOver = (e) => {
810
if (this.preLoading) {
911
return
1012
}
1113
this.preLoading = true
1214
this.vm[this.expression].call(null, this, e)
1315
}
16+
this.handleMouseLeave = (e) => {
17+
this.mouseOver = false
18+
this.firstBlood = false
19+
}
1420
this.handleClick = (e) => {
1521
e.preventDefault()
16-
for(const key in this.tmp) {
17-
this.vm.$set(key, this.tmp[key])
22+
if (this.mouseOver && this.firstBlood) {
23+
return this.handleMouseOver(e)
1824
}
25+
this.firstBlood = true
26+
this.setState(this.tmp)
1927
}
2028
_.on(this.el, 'mouseover', this.handleMouseOver)
29+
_.on(this.el, 'mouseleave', this.handleMouseLeave)
2130
_.on(this.el, 'click', this.handleClick)
2231
},
2332
set(obj) {
2433
this.tmp = obj
2534
},
35+
setState(state) {
36+
for(const key in state) {
37+
this.vm.$set(key, state[key])
38+
}
39+
},
2640
end() {
2741
this.preLoading = false
42+
if (this.mouseOver) {
43+
this.setState(this.tmp)
44+
}
45+
this.mouseOver = true
2846
},
2947
reset() {
3048
_.off(this.el, 'mouseover', this.handleMouseOver)
49+
_.off(this.el, 'mouseleave', this.handleMouseLeave)
50+
_.off(this.el, 'click', this.handleClick)
3151
},
3252
unbind() {
3353
this.reset()

0 commit comments

Comments
 (0)