Skip to content

Commit 67dc3ef

Browse files
committed
Initial sources
1 parent 120e5e6 commit 67dc3ef

File tree

6 files changed

+472
-2
lines changed

6 files changed

+472
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
lib/

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/

README.md

Lines changed: 264 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,264 @@
1-
# vue-meteor-integration
2-
Use Meteor Tracker reactivity inside Vue components
1+
# Vue integration for Meteor
2+
3+
Compatibility: **Vue 1.x, Vue 2.x**
4+
5+
Declarative subscriptions and meteor reactive data (subscriptions, collections, tracker...)
6+
7+
[Example project](https://github.com/Akryum/meteor-vue-example)
8+
9+
## Installation
10+
11+
12+
npm install --save vue-meteor-tracker
13+
14+
## Usage
15+
16+
```javascript
17+
import VueMeteorTracker from 'vue-meteor-tracker';
18+
Vue.use(VueMeteorTracker);
19+
```
20+
21+
In your Vue component, add a `meteor` object :
22+
23+
24+
```javascript
25+
new Vue({
26+
meteor: {
27+
// Meteor specific options
28+
}
29+
});
30+
```
31+
32+
33+
#### Subscriptions
34+
35+
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.
36+
37+
```javascript
38+
meteor: {
39+
// Subscriptions
40+
subscribe: {
41+
// Subscribes to the 'threads' publication with no parameters
42+
'threads': [],
43+
// Subscribes to the 'threads' publication with static parameters
44+
'threads': ['new', 10], // The 10 newest threads
45+
// Subscribes to the 'posts' publication with dynamic parameters
46+
// The subscription will be re-called when a vue reactive property changes
47+
'posts': function() {
48+
// Here you can use Vue reactive properties
49+
return [this.selectedThreadId] // Subscription params
50+
}
51+
}
52+
}
53+
```
54+
55+
56+
You can also use the `$subscribe(name, ...params)` method in you component code:
57+
58+
59+
```javascript
60+
ready () {
61+
// Subscribes to the 'threads' publication with two parameters
62+
this.$subscribe('thread', 'new', 10);
63+
}
64+
```
65+
66+
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:
67+
68+
```javascript
69+
console.log(this.$subReady.thread);
70+
```
71+
72+
Or in your template:
73+
74+
```html
75+
<div v-if="!$subReady.thread">Loading...</div>
76+
```
77+
78+
You can also change the default subscription method by defining the `Vue.config.meteor.subscribe` function:
79+
80+
81+
```javascript
82+
// You can replace the default subcription function with your own
83+
// Here we replace the native subscribe() with a cached one
84+
// with the ccorcos:subs-cache package
85+
const subsCache = new SubsCache({
86+
expireAfter: 15,
87+
cacheLimit: -1
88+
});
89+
90+
Vue.config.meteor.subscribe = function(...args) {
91+
return subsCache.subscribe(...args);
92+
};
93+
```
94+
95+
#### Reactive data
96+
97+
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 `data` object. The object key is the name of the property, and the value is either a function or an object with the following attributes:
98+
99+
- `params()` (optional), a function returning an object, which can use any *Vue* reactive property,
100+
- `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.
101+
102+
Here is an example:
103+
104+
```javascript
105+
new Vue({
106+
data() {
107+
return {
108+
selectedThreadId: null,
109+
// We can init the property value in the data() component hook
110+
threads: [],
111+
selectedThread: null
112+
};
113+
},
114+
meteor: {
115+
// Subscriptions
116+
subscribe: {
117+
// We subscribe to the 'threads' publication
118+
'threads': []
119+
},
120+
data: {
121+
// Threads list
122+
// This will update the 'threads' array property on the Vue instance
123+
// that we set in the data() hook earlier
124+
// You can use a function directly if you don't need
125+
// parameters coming from the Vue instance
126+
threads () {
127+
// Here you can use Meteor reactive sources
128+
// like cursors or reactive vars
129+
// as you would in a Blaze template helper
130+
// However, Vue reactive properties will not update
131+
return Threads.find({}, {
132+
sort: {date: -1}
133+
});
134+
},
135+
// Selected thread
136+
// This will update the 'selectedThread' object property on component
137+
selectedThread: {
138+
//// Vue Reactivity
139+
// We declare which params depends on reactive vue properties
140+
params () {
141+
// Here you can use Vue reactive properties
142+
// Don't use Meteor reactive sources!
143+
return {
144+
id: this.selectedThreadId
145+
};
146+
},
147+
//// Meteor Reactivity
148+
// This will be refresh each time above params changes from Vue
149+
// Then it calls Tracker.autorun() to refresh the result
150+
// each time a Meteor reactive source changes
151+
update ({id}) {
152+
// Here you can use Meteor reactive sources
153+
// like cursors or reactive vars
154+
// Don't use Vue reactive properties!
155+
return Threads.findOne(id);
156+
},
157+
},
158+
},
159+
},
160+
});
161+
```
162+
163+
You can skip the data initialization (the default value will be `null`) and the `data` property (as long as none of your meteor props is named 'data'):
164+
165+
```javascript
166+
new Vue({
167+
data() {
168+
return {
169+
selectedThreadId: null,
170+
};
171+
},
172+
meteor: {
173+
// Subscriptions
174+
subscribe: {
175+
// We subscribe to the 'threads' publication
176+
'threads': []
177+
},
178+
// Threads list
179+
// This will update the 'threads' array property on the Vue instance
180+
// that we set in the data() hook earlier
181+
// You can use a function directly if you don't need
182+
// parameters coming from the Vue instance
183+
threads () {
184+
// Here you can use Meteor reactive sources
185+
// like cursors or reactive vars
186+
// as you would in a Blaze template helper
187+
// However, Vue reactive properties will not update
188+
return Threads.find({}, {
189+
sort: {date: -1}
190+
});
191+
},
192+
// Selected thread
193+
// This will update the 'selectedThread' object property on component
194+
selectedThread: {
195+
//// Vue Reactivity
196+
// We declare which params depends on reactive vue properties
197+
params () {
198+
// Here you can use Vue reactive properties
199+
// Don't use Meteor reactive sources!
200+
return {
201+
id: this.selectedThreadId
202+
};
203+
},
204+
//// Meteor Reactivity
205+
// This will be refresh each time above params changes from Vue
206+
// Then it calls Tracker.autorun() to refresh the result
207+
// each time a Meteor reactive source changes
208+
update ({id}) {
209+
// Here you can use Meteor reactive sources
210+
// like cursors or reactive vars
211+
// Don't use Vue reactive properties!
212+
return Threads.findOne(id);
213+
},
214+
},
215+
},
216+
});
217+
```
218+
219+
You can then use the reactive data in the template since it's standard Vue component properties:
220+
221+
222+
```html
223+
<!-- Thread list -->
224+
<thread-item v-for="thread in threads" :data="thread" :selected="thread._id === selectedThreadId" @select="selectThread(thread._id)"></thread-item>
225+
226+
<!-- Selected thread -->
227+
<thread v-if="selectedThread" :id="selectedThreadId"></thread>
228+
```
229+
230+
231+
Or anywhere else in you Vue component:
232+
233+
```javascript
234+
computed: {
235+
count () {
236+
return this.threads.length;
237+
}
238+
}
239+
```
240+
241+
#### Freezing data
242+
243+
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.
244+
245+
```javascript
246+
// Disable Vue reactivity on Meteor data
247+
Vue.config.meteor.freeze = true;
248+
```
249+
250+
---
251+
252+
## Next steps
253+
254+
- [Write your components in vue files](https://github.com/Akryum/meteor-vue-component/tree/master/packages/vue-component#usage)
255+
- [Example project without blaze](https://github.com/Akryum/meteor-vue-example)
256+
- [Example project with blaze](https://github.com/Akryum/meteor-vue-blaze)
257+
- [Add routing to your app](https://github.com/Akryum/meteor-vue-component/tree/master/packages/vue-router#installation)
258+
- [Add internationalization to your app](https://github.com/Akryum/meteor-vue-component/tree/master/packages/vue-i18n#installation)
259+
- [Manage your app state with a vuex store](https://github.com/Akryum/meteor-vue-component/tree/master/packages/vuex#installation)
260+
- [Integrate apollo](https://github.com/Akryum/meteor-vue-component/tree/master/packages/vue-apollo#installation)
261+
262+
---
263+
264+
LICENCE ISC - Created by Guillaume CHAU (@Akryum)

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export VueMeteor from './src/vue-plugin';

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "vue-meteor-tracker",
3+
"version": "1.0.0",
4+
"description": "Use Meteor Tracker reactivity inside Vue components",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/Akryum/vue-meteor-integration.git"
12+
},
13+
"keywords": [
14+
"vue",
15+
"meteor",
16+
"integration",
17+
"tracker",
18+
"reactivity"
19+
],
20+
"author": "Guillaume CHAU",
21+
"license": "ISC",
22+
"bugs": {
23+
"url": "https://github.com/Akryum/vue-meteor-integration/issues"
24+
},
25+
"homepage": "https://github.com/Akryum/vue-meteor-integration#readme",
26+
"dependencies": {
27+
"lodash.omit": "^4.5.0"
28+
}
29+
}

0 commit comments

Comments
 (0)