Skip to content

Commit a71af7a

Browse files
authored
Merge pull request #1 from Komar12124/master
Add computed channel name and remove vm from event listeners
2 parents c32478b + a9bf0ae commit a71af7a

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

README.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ You can subscribe a vue instance to a single standard channel if needed and defi
6060
var vm = new Vue({
6161
channel: 'blog',
6262
echo: {
63-
'BlogPostCreated': (payload, vm) => {
63+
'BlogPostCreated': (payload) => {
6464
console.log('blog post created', payload);
6565
},
66-
'BlogPostDeleted': (payload, vm) => {
66+
'BlogPostDeleted': (payload) => {
6767
console.log('blog post deleted', payload);
6868
}
6969
}
7070
});
7171
```
7272

73-
Since the scope of `this` would be the same as the scope where you declare your Vue instance a second parameter is added to these locally registered events. This parameter is a direct reference to your Vue instance, you can make any changes you need through there.
73+
You can feel free to use `this` inside Your methods.
7474

7575
#### Subscribing to channels
7676

@@ -85,16 +85,41 @@ If you would like to subscribe to a private channel instead, prefix your channel
8585
var vm = new Vue({
8686
channel: 'private:user.1',
8787
echo: {
88-
'BlogPostCreated': (payload, vm) => {
88+
'BlogPostCreated': (payload) => {
8989
console.log('blog post created', payload);
9090
},
91-
'BlogPostDeleted': (payload, vm) => {
91+
'BlogPostDeleted': (payload) => {
9292
console.log('blog post deleted', payload);
9393
}
9494
}
9595
});
9696
```
9797

98+
If you need to compute channel name, you should pass `channel` as function.
99+
100+
```js
101+
var vm = new Vue({
102+
channel() {
103+
return `private:user.${ this.userId }`
104+
},
105+
echo: {
106+
'BlogPostCreated': (payload) => {
107+
console.log('blog post created', payload);
108+
},
109+
'BlogPostDeleted': (payload) => {
110+
console.log('blog post deleted', payload);
111+
}
112+
},
113+
114+
computed: {
115+
userId() {
116+
return 1;
117+
}
118+
}
119+
});
120+
````
121+
122+
98123
##### Presence channel
99124

100125
If you would like to subscribe to presence channel instead, prefix your channel name with `presence:`
@@ -103,7 +128,7 @@ If you would like to subscribe to presence channel instead, prefix your channel
103128
var vm = new Vue({
104129
channel: 'presence:user.1.chat',
105130
echo: {
106-
'NewMessage': (payload, vm) => {
131+
'NewMessage': (payload) => {
107132
console.log('new message from team', payload);
108133
}
109134
}
@@ -118,10 +143,10 @@ If there's a scenario where you want to listen to events after certain condition
118143
var vm = new Vue({
119144
channel: 'private:user.1',
120145
echo: {
121-
'BlogPostCreated': (payload, vm) => {
146+
'BlogPostCreated': (payload) => {
122147
console.log('blog post created', payload);
123148
},
124-
'BlogPostDeleted': (payload, vm) => {
149+
'BlogPostDeleted': (payload) => {
125150
console.log('blog post deleted', payload);
126151
}
127152
},

0 commit comments

Comments
 (0)