Skip to content

Commit eb3c2ba

Browse files
committed
docs Model class event handlers
1 parent 6460df8 commit eb3c2ba

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

docs/model-classes.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,46 @@ A new `setupinstance` class method is now available in version 2.0. The functio
131131
- `store` - The vuex store
132132
- `Models {Object}` The `globalModels` object, which is the same as you'll find inside a component at `this.$FeathersVuex`.
133133

134+
### on <Badge text="2.3.0+" />
135+
136+
Register event handlers to listen to events.
137+
138+
### once <Badge text="2.3.0+" />
139+
140+
Register an event handler that only occurs once.
141+
142+
### off <Badge text="2.3.0+" />
143+
144+
Remove an event handler.
145+
146+
## Model Events <Badge text="2.3.0+" />
147+
148+
Model classes are EventEmitter instances which emit service events when received (technically, EventEmitter methods are mixed onto each Model class). All FeathersJS events are supported. Here’s an example of how to use it in a component:
149+
150+
```js
151+
export default {
152+
created() {
153+
this.$FeathersVuex.api.Todo.on(‘created’, this.handleTodoCreated)
154+
},
155+
methods: {
156+
handleTodoCreated(todo) {
157+
console.log(todo)
158+
}
159+
}
160+
}
161+
```
162+
163+
Since they have all of the EventEmitter methods, Model classes can be used as a data-layer Event Bus. You can even use custom methods:
164+
165+
```
166+
const { Todo } = this.$FeathersVuex.api
167+
168+
Todo.on('custom-event', data => {
169+
console.log(data) // { test: true }
170+
})
171+
172+
Todo.emit('custom-event', { test: true })
173+
```
134174

135175
## Creating instances
136176

0 commit comments

Comments
 (0)