You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*Note: if you are using the Meteor [akryum:vue](https://github.com/Akryum/meteor-vue-component/tree/master/packages/vue) package, you don't need to install the plugin.*
@@ -29,32 +29,33 @@ Vue.use(VueMeteorTracker);
29
29
In your Vue component, add a `meteor` object :
30
30
31
31
32
-
```javascript
33
-
newVue({
32
+
```js
33
+
exportdefault{
34
34
meteor: {
35
35
// Meteor specific options
36
36
}
37
-
});
37
+
}
38
38
```
39
39
40
-
41
40
#### Subscriptions
42
41
43
42
Add an object for each subscription in a `$subscribe` object. The object key is the name of the publication and the value is either an array of parameters or a function returning an array of parameters. These subscription will be stopped when the component is destroyed.
44
43
45
-
```javascript
46
-
meteor: {
47
-
// Subscriptions
48
-
$subscribe: {
49
-
// Subscribes to the 'threads' publication with no parameters
50
-
'threads': [],
51
-
// Subscribes to the 'threads' publication with static parameters
52
-
'threads': ['new', 10], // The 10 newest threads
53
-
// Subscribes to the 'posts' publication with dynamic parameters
54
-
// The subscription will be re-called when a vue reactive property changes
You can also use the `$subscribe(name, ...params)` method in you component code:
65
66
66
67
67
-
```javascript
68
+
```js
68
69
ready () {
69
70
// Subscribes to the 'threads' publication with two parameters
70
-
this.$subscribe('thread', 'new', 10);
71
+
this.$subscribe('thread', 'new', 10)
71
72
}
72
73
```
73
74
74
75
The `$subReady` object on your component contains the state of your subscriptions. For example, to know if the 'thread' subscription is ready, use this *reactive* expression:
75
76
76
-
```javascript
77
-
console.log(this.$subReady.thread);
77
+
```js
78
+
console.log(this.$subReady.thread)
78
79
```
79
80
80
81
Or in your template:
@@ -86,38 +87,35 @@ Or in your template:
86
87
You can also change the default subscription method by defining the `Vue.config.meteor.subscribe` function:
87
88
88
89
89
-
```javascript
90
+
```js
90
91
// You can replace the default subcription function with your own
91
92
// Here we replace the native subscribe() with a cached one
92
93
// with the ccorcos:subs-cache package
93
94
constsubsCache=newSubsCache({
94
95
expireAfter:15,
95
96
cacheLimit:-1
96
-
});
97
+
})
97
98
98
99
Vue.config.meteor.subscribe=function(...args) {
99
-
returnsubsCache.subscribe(...args);
100
-
};
100
+
returnsubsCache.subscribe(...args)
101
+
}
101
102
```
102
103
103
104
#### Reactive data
104
105
105
-
You can make your component `data` properties update from any Meteor reactive sources (like collections or session) by putting an object for each property in the `meteor` object. The object key is the name of the property (it shouldn't start with `$`), and the value is either a function or an object with the following attributes:
106
-
107
-
-`params()` (optional), a function returning an object, which can use any *Vue* reactive property,
108
-
-`update([params])`, a function with optional `params` argument, that returns the value to update the corresponding `data` property of the component. Here you can use *Meteor* reactive sources, but **no Vue reactive property getters**. The `params` argument is the object returned by the `params()` function described above.
106
+
You can make your component `data` properties update from any Meteor reactive sources (like collections or session) by putting an object for each property in the `meteor` object. The object key is the name of the property (it shouldn't start with `$`), and the value is a function.
109
107
110
108
Here is an example:
111
109
112
-
```javascript
113
-
newVue({
110
+
```js
111
+
exportdefault{
114
112
data() {
115
113
return {
116
114
selectedThreadId:null,
117
115
// We can init the property value in the data() component hook
118
116
threads: [],
119
117
selectedThread:null
120
-
};
118
+
}
121
119
},
122
120
meteor: {
123
121
// Subscriptions
@@ -134,77 +132,22 @@ new Vue({
134
132
// Here you can use Meteor reactive sources
135
133
// like cursors or reactive vars
136
134
// as you would in a Blaze template helper
137
-
// However, Vue reactive properties will not update
138
135
returnThreads.find({}, {
139
136
sort: {date:-1}
140
-
});
137
+
})
141
138
},
142
139
// Selected thread
143
140
// This will update the 'selectedThread' object property on component
144
-
selectedThread: {
145
-
//// Vue Reactivity
146
-
// We declare which params depends on reactive vue properties
147
-
params () {
148
-
// Here you can use Vue reactive properties
149
-
// Don't use Meteor reactive sources!
150
-
return {
151
-
id:this.selectedThreadId
152
-
};
153
-
},
154
-
// Optionally we can watch the parameters for changes in nested
155
-
// objects using the 'deep' option
156
-
deep:true,
157
-
//// Meteor Reactivity
158
-
// This will be refresh each time above params changes from Vue
159
-
// Then it calls Tracker.autorun() to refresh the result
160
-
// each time a Meteor reactive source changes
161
-
update ({id}) {
162
-
// Here you can use Meteor reactive sources
163
-
// like cursors or reactive vars
164
-
// Don't use Vue reactive properties!
165
-
returnThreads.findOne(id);
166
-
},
167
-
},
168
-
},
169
-
});
141
+
selectedThread () {
142
+
returnThreads.findOne(this.selectedThreadId)
143
+
}
144
+
}
145
+
})
170
146
```
171
147
172
-
You can skip the data initialization (the default value will be `null`):
173
-
174
-
```javascript
175
-
newVue({
176
-
data() {
177
-
return {
178
-
selectedThreadId:null,
179
-
};
180
-
},
181
-
meteor: {
182
-
// Subscriptions
183
-
$subscribe: {
184
-
'threads': []
185
-
},
186
-
// Threads list
187
-
threads () {
188
-
returnThreads.find({}, {
189
-
sort: {date:-1}
190
-
});
191
-
},
192
-
// Selected thread
193
-
selectedThread: {
194
-
params () {
195
-
return {
196
-
id:this.selectedThreadId
197
-
};
198
-
},
199
-
update ({id}) {
200
-
returnThreads.findOne(id);
201
-
},
202
-
},
203
-
},
204
-
});
205
-
```
148
+
You can skip the data initialization (the default value will be `null`).
206
149
207
-
You can then use the reactive data in the template since it's standard Vue component properties:
150
+
Use the reactive data in the template:
208
151
209
152
210
153
```html
@@ -218,10 +161,10 @@ You can then use the reactive data in the template since it's standard Vue compo
218
161
219
162
Or anywhere else in you Vue component:
220
163
221
-
```javascript
164
+
```js
222
165
computed: {
223
166
count () {
224
-
returnthis.threads.length;
167
+
returnthis.threads.length
225
168
}
226
169
}
227
170
```
@@ -230,7 +173,7 @@ computed: {
230
173
231
174
You can deactivate and activate again the meteor data on the component with `this.$startMeteor` and `this.$stopMeteor`:
232
175
233
-
```javascript
176
+
```js
234
177
exportdefault {
235
178
meteor: {
236
179
// ...
@@ -243,29 +186,81 @@ export default {
243
186
244
187
deactivate () {
245
188
this.$stopMeteor()
246
-
},
247
-
},
189
+
}
190
+
}
248
191
}
249
192
```
250
193
251
194
You can also prevent meteor data from starting automatically with `$lazy`:
252
195
253
-
```javascript
196
+
```js
254
197
exportdefault {
255
198
meteor: {
256
199
$lazy:true,
257
200
// ...
258
-
},
201
+
}
259
202
}
260
203
```
261
204
262
205
#### Freezing data
263
206
264
207
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.
265
208
266
-
```javascript
209
+
```js
267
210
// Disable Vue reactivity on Meteor data
268
-
Vue.config.meteor.freeze=true;
211
+
Vue.config.meteor.freeze=true
212
+
```
213
+
214
+
### Components
215
+
216
+
**Vue 2+ only**
217
+
218
+
You can use Meteor directly in the template using the Meteor components and scoped slots:
0 commit comments