on.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var utils = require('../utils')
  2. /**
  3. * Binding for event listeners
  4. */
  5. module.exports = {
  6. isFn: true,
  7. bind: function () {
  8. this.context = this.binding.isExp
  9. ? this.vm
  10. : this.binding.compiler.vm
  11. if (this.el.tagName === 'IFRAME' && this.arg !== 'load') {
  12. var self = this
  13. this.iframeBind = function () {
  14. self.el.contentWindow.addEventListener(self.arg, self.handler)
  15. }
  16. this.el.addEventListener('load', this.iframeBind)
  17. }
  18. },
  19. update: function (handler) {
  20. if (typeof handler !== 'function') {
  21. utils.warn('Directive "v-on:' + this.expression + '" expects a method.')
  22. return
  23. }
  24. this.reset()
  25. var vm = this.vm,
  26. context = this.context
  27. this.handler = function (e) {
  28. e.targetVM = vm
  29. context.$event = e
  30. var res = handler.call(context, e)
  31. context.$event = null
  32. return res
  33. }
  34. if (this.iframeBind) {
  35. this.iframeBind()
  36. } else {
  37. this.el.addEventListener(this.arg, this.handler)
  38. }
  39. },
  40. reset: function () {
  41. var el = this.iframeBind
  42. ? this.el.contentWindow
  43. : this.el
  44. el.removeEventListener(this.arg, this.handler)
  45. },
  46. unbind: function () {
  47. this.reset()
  48. this.el.removeEventListener('load', this.iframeBind)
  49. }
  50. }