Skip to content

Commit cb87986

Browse files
author
Guillaume Chau
committed
Activation/Deactivation & $lazy
1 parent eee1d07 commit cb87986

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,39 @@ computed: {
226226
}
227227
```
228228

229+
#### Activating and deactivating meteor data
230+
231+
You can deactivate and activate again the meteor data on the component with `this.$startMeteor` and `this.$stopMeteor`:
232+
233+
```javascript
234+
export default {
235+
meteor: {
236+
// ...
237+
},
238+
239+
methods: {
240+
activate () {
241+
this.$startMeteor()
242+
},
243+
244+
deactivate () {
245+
this.$stopMeteor()
246+
},
247+
},
248+
}
249+
```
250+
251+
You can also prevent meteor data from starting automatically with `$lazy`:
252+
253+
```javascript
254+
export default {
255+
meteor: {
256+
$lazy: true,
257+
// ...
258+
},
259+
}
260+
```
261+
229262
#### Freezing data
230263

231264
This option will apply `Object.freeze` on the Meteor data to prevent Vue from setting up reactivity on it. This can improve the performance of Vue when rendering large collection lists for example. By default, this option is turned off.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-meteor-tracker",
3-
"version": "1.2.1",
3+
"version": "1.2.3",
44
"description": "Use Meteor Tracker reactivity inside Vue components",
55
"main": "index.js",
66
"scripts": {

src/vue-plugin.js

+30-10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export default {
5151

5252
function launch() {
5353

54+
this._meteorActive = true
55+
5456
let meteor = this.$options.meteor;
5557

5658
if (meteor) {
@@ -161,18 +163,14 @@ export default {
161163
// Vue 2.x
162164
beforeCreate: prepare,
163165

164-
created: launch,
166+
created () {
167+
if (this.$options.meteor && !this.$options.meteor.$lazy) {
168+
launch.call(this)
169+
}
170+
},
165171

166172
destroyed: function() {
167-
//Stop all reactivity when view is destroyed.
168-
this._trackerHandles.forEach((tracker) => {
169-
try {
170-
tracker.stop()
171-
} catch (e) {
172-
console.error(e, tracker)
173-
}
174-
})
175-
this._trackerHandles = null
173+
this.$stopMeteor()
176174
},
177175

178176
methods: {
@@ -233,6 +231,28 @@ export default {
233231
}
234232
},
235233

234+
$startMeteor () {
235+
if (!this._meteorActive) {
236+
prepare.call(this)
237+
launch.call(this)
238+
}
239+
},
240+
241+
$stopMeteor () {
242+
if (this._meteorActive) {
243+
//Stop all reactivity when view is destroyed.
244+
this._trackerHandles.forEach((tracker) => {
245+
try {
246+
tracker.stop()
247+
} catch (e) {
248+
console.error(e, tracker)
249+
}
250+
})
251+
this._trackerHandles = null
252+
this._meteorActive = false
253+
}
254+
},
255+
236256
},
237257

238258
});

0 commit comments

Comments
 (0)