8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vueで自前のdirectiveを登録する

Last updated at Posted at 2014-07-08

クリックしたらその名前のイベントを発火するだけのディレクティブを作った。自分は処理の委譲の為にこれ作らんと話にならなかったので。

Vue.directive 'dispatcher',
  isLiteral: true
  bind: (value) ->
    @el.addEventListener 'click', =>
      @vm.$dispatch(@expression)

v = new Vue
  template: """<button v-dispatcher="message"></button>"""
  
v.$on 'message', ->
  console.log 'message clicked'

最初ViewModelのインスタンスを得る方法がわからなかったのだけど、リファレンス読んだらちゃんと書いてあった。

el: the element the directive is bound to.
key: the keypath of the binding, excluding arguments and filters.
arg: the argument, if present.
expression: the raw, unparsed expression.
vm: the context ViewModel that owns this directive.
value: the current binding value.

Directives In Depth - vue.js

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?