Skip to content

Commit

Permalink
add once method
Browse files Browse the repository at this point in the history
  • Loading branch information
yangmingshan authored and yangmingshan committed Nov 11, 2016
1 parent d237b13 commit 1660fb5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ You don't need to do this when using global script tags.
// ...
created() {
this.$bus.on('add-todo', this.addTodo);
this.$bus.once('once', () => console.log('This listener will only fire once'));
},
beforeDestroy() {
this.$bus.off('add-todo', this.addTodo);
},
methods: {
addTodo(newTodo) {
this.todos.push(newTodo)
this.todos.push(newTodo);
}
}
```
Expand All @@ -36,10 +37,12 @@ methods: {
methods: {
addTodo() {
this.$bus.emit('add-todo', { text: this.newTodoText });
this.$bus.emit('once');
this.newTodoText = '';
}
}
```
*Note: `$bus.on` `$bus.once` `$bus.off` `$bus.emit` are aliases for `$bus.$on` `$bus.$once` `$bus.$off` `$bus.$emit`. See the [API](http://vuejs.org/v2/api/#Instance-Methods-Events) for more detail.*

## License
[MIT](http://opensource.org/licenses/MIT)
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
return this.$on;
}
},
once: {
get: function() {
return this.$once;
}
},
off: {
get: function() {
return this.$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-bus",
"version": "0.2.0",
"version": "0.3.0",
"description": "A event bus for Vue.js",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 1660fb5

Please sign in to comment.