Skip to content

Commit

Permalink
Activation/Deactivation & $lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Jun 2, 2017
1 parent eee1d07 commit cb87986
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,39 @@ computed: {
}
```

#### Activating and deactivating meteor data

You can deactivate and activate again the meteor data on the component with `this.$startMeteor` and `this.$stopMeteor`:

```javascript
export default {
meteor: {
// ...
},

methods: {
activate () {
this.$startMeteor()
},

deactivate () {
this.$stopMeteor()
},
},
}
```

You can also prevent meteor data from starting automatically with `$lazy`:

```javascript
export default {
meteor: {
$lazy: true,
// ...
},
}
```

#### Freezing data

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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-meteor-tracker",
"version": "1.2.1",
"version": "1.2.3",
"description": "Use Meteor Tracker reactivity inside Vue components",
"main": "index.js",
"scripts": {
Expand Down
40 changes: 30 additions & 10 deletions src/vue-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export default {

function launch() {

this._meteorActive = true

let meteor = this.$options.meteor;

if (meteor) {
Expand Down Expand Up @@ -161,18 +163,14 @@ export default {
// Vue 2.x
beforeCreate: prepare,

created: launch,
created () {
if (this.$options.meteor && !this.$options.meteor.$lazy) {
launch.call(this)
}
},

destroyed: function() {
//Stop all reactivity when view is destroyed.
this._trackerHandles.forEach((tracker) => {
try {
tracker.stop()
} catch (e) {
console.error(e, tracker)
}
})
this._trackerHandles = null
this.$stopMeteor()
},

methods: {
Expand Down Expand Up @@ -233,6 +231,28 @@ export default {
}
},

$startMeteor () {
if (!this._meteorActive) {
prepare.call(this)
launch.call(this)
}
},

$stopMeteor () {
if (this._meteorActive) {
//Stop all reactivity when view is destroyed.
this._trackerHandles.forEach((tracker) => {
try {
tracker.stop()
} catch (e) {
console.error(e, tracker)
}
})
this._trackerHandles = null
this._meteorActive = false
}
},

},

});
Expand Down

0 comments on commit cb87986

Please sign in to comment.