Skip to content

Commit 34b7773

Browse files
author
Marty Wallace
committed
Provide custom functionality to events.
1 parent 88226c5 commit 34b7773

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ The `chars` property accepts a pipe `|` delimited string of characters to use as
1414

1515
* `backspace` - Remove one character from the end of the current value.
1616
* `space` - Insert one whitespace character.
17-
* `clear` - Clear the entire input value.
17+
* `clear` - Clear the entire input value.
18+
19+
> If the `action` does not match any of these inbuilt actions, an event will be dispatched by the keyboard component instead, using the action name. The keyboard component will be provided to the listener as the first argument.
20+
21+
Buttons rendered with this syntax will include a class name `action-<x>` where `<x>` is the name of the action e.g. `action-clear`.

demo/basic.html

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<title>Keyboard Demo</title>
66
</head>
77
<body>
8-
<keyboard chars="qwertyuiop{clear:clear}|asdfghjkl{backspace:backspace}|zxcvbnm|{space:space}" :value.sync="input"></keyboard>
8+
<keyboard chars="qwertyuiop{clear:clear}|asdfghjkl{backspace:backspace}|zxcvbnm|{space:space}{test:test}" :value.sync="input"></keyboard>
99
<p>{{ input }}</p>
1010

1111
<script src="../bower_components/vue/dist/vue.min.js"></script>
@@ -14,7 +14,14 @@
1414
<script>
1515
var app = new Vue({
1616
el: 'body',
17-
input: ''
17+
data: {
18+
input: ''
19+
},
20+
events: {
21+
test: function(keyboard) {
22+
console.log(keyboard.value);
23+
}
24+
}
1825
});
1926
</script>
2027
</body>

src/vue-keyboard.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@
2727
token = '';
2828
} else if (char === '}') {
2929
var command = (/(\w+):(\w+)/g).exec(token);
30+
var action = null;
31+
32+
if (this.hasOwnProperty(command[2])) {
33+
action = this[command[2]].bind(this);
34+
} else {
35+
action = this.$dispatch.bind(this, command[2], this);
36+
}
3037

3138
buttons.push({
3239
class: 'action-' + command[2].replace(/\s+/g, '-').toLowerCase(),
33-
action: this[command[2]].bind(this),
40+
action: action,
3441
value: command[1]
3542
});
3643

0 commit comments

Comments
 (0)