Skip to content

Commit ffe4ee8

Browse files
committed
1. bug fix
2. add option "regGlobal"
1 parent 6e4f420 commit ffe4ee8

File tree

7 files changed

+40
-18
lines changed

7 files changed

+40
-18
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ Vue.use(VueLLazyload, config);
133133
By default, it will be set as the "src" attribute of the element.
134134
</td>
135135
</tr>
136+
<tr>
137+
<td>regGlobal</td>
138+
<td>Boolean</td>
139+
<td>true</td>
140+
<td>
141+
Whether register the directives and components globally or not.
142+
</td>
143+
</tr>
136144
</tobdy>
137145
</table>
138146
[1]: All options should not be changed after they have initialized except for [2].

demo/src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import app from './App'
33
import { VueLLazyload } from '../../src/index';
44

55
Vue.use(VueLLazyload, {
6-
once: false
6+
once: false,
77
});
88

99
new Vue({

dist/main.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main.js.gz

41 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-l-lazyload",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A lazyload plugin for Vue.js v2.x+.",
55
"main": "dist/main.js",
66
"directories": {

src/index.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ const Lazy = {
6666
};
6767
}
6868

69-
// vnode will be recreated during update, so bind props with real element
70-
el._lazyBound = true;
69+
vnode._lazyBound = true;
7170

7271
const vm = vnode.context,
7372
refStr = opts.ref;
@@ -76,7 +75,7 @@ const Lazy = {
7675
vm.$nextTick(() => {
7776
var ref;
7877
// Prevent it's unbound before initialization
79-
if (el._lazyBound) {
78+
if (vnode._lazyBound) {
8079
if (refStr) {
8180
ref = vm.$refs[refStr];
8281
if (!ref) {
@@ -94,20 +93,26 @@ const Lazy = {
9493
mergedOpts.parent = $lazy;
9594
}
9695

97-
const loader = el._$lazy = new LazyLoader(mergedOpts);
96+
const loader = vnode._$lazy = new LazyLoader(mergedOpts);
9897

9998
loader.check();
10099
}
101100
});
102101
},
103-
componentUpdated(el, binding) {
102+
componentUpdated(el, binding, vnode, ovnode) {
104103
var opts = binding.value,
105104
oOpts = binding.oldValue,
106105
nSrc = isStr(opts) ? opts : opts.src,
107106
oSrc = isStr(oOpts) ? oOpts : oOpts.src;
108107

108+
// vnode will be recreated during update
109+
if (vnode !== ovnode) {
110+
vnode._lazyBound = ovnode._lazyBound;
111+
vnode._$lazy = ovnode._$lazy;
112+
}
113+
109114
if (nSrc != oSrc) {
110-
const loader = el._$lazy;
115+
const loader = vnode._$lazy;
111116

112117
if (loader) {
113118
loader.update({
@@ -117,31 +122,40 @@ const Lazy = {
117122
}
118123
},
119124
unbind(el, binding, vnode) {
120-
if (el._lazyBound) {
121-
el._lazyBound = false;
125+
if (vnode._lazyBound) {
126+
vnode._lazyBound = false;
122127
}
123128

129+
console.log(vnode);
130+
124131
vnode.context.$nextTick(() => {
125-
const loader = el._$lazy;
132+
const loader = vnode._$lazy;
126133
if (loader) {
127134
loader.destroy();
128-
el._$lazy = null;
135+
vnode._$lazy = null;
129136
}
130137
});
131138
},
132139
};
133140

134141
const VueLLazyload = {
135-
install(Vue, options = {}) {
142+
install(Vue, options) {
143+
const allOpts = {
144+
regGlobal: true,
145+
...options,
146+
};
147+
136148
LazyLoader = LazyClass(Vue);
137149
Vue.$lazy = new LazyLoader({
138150
...options,
139151
isRoot: true,
140152
});
141153

142-
Vue.directive('lazy', Lazy);
154+
if (allOpts.regGlobal) {
155+
Vue.directive('lazy', Lazy);
143156

144-
Vue.component('lazy-ref', LazyRef);
157+
Vue.component('lazy-ref', LazyRef);
158+
}
145159
},
146160
};
147161

src/lazy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export function LazyClass(scope) {
231231
var i,
232232
len;
233233

234-
me.stat < STAT_LOADED && me.inView() && loadHandler(me);
234+
me.stat < STAT_LOADING && me.inView() && loadHandler(me);
235235

236236
const children = me._children;
237237

0 commit comments

Comments
 (0)