Browse Source

v-on: support modifiers without expression (close #1451)

Evan You 10 years ago
parent
commit
d1ab4245eb
3 changed files with 17 additions and 1 deletions
  1. 1 1
      src/directive.js
  2. 6 0
      src/directives/public/on.js
  3. 10 0
      test/unit/specs/directives/public/on_spec.js

+ 1 - 1
src/directive.js

@@ -88,7 +88,7 @@ Directive.prototype._bind = function () {
   if (this.literal) {
     this.update && this.update(descriptor.raw)
   } else if (
-    this.expression &&
+    (this.expression || this.modifiers) &&
     (this.update || this.twoWay) &&
     !this._checkStatement()
   ) {

+ 6 - 0
src/directives/public/on.js

@@ -62,6 +62,12 @@ module.exports = {
   },
 
   update: function (handler) {
+    // stub a noop for v-on with no value,
+    // e.g. @mousedown.prevent
+    if (!this.descriptor.raw) {
+      handler = function () {}
+    }
+
     if (typeof handler !== 'function') {
       process.env.NODE_ENV !== 'production' && _.warn(
         'v-on:' + this.arg + '="' +

+ 10 - 0
test/unit/specs/directives/public/on_spec.js

@@ -144,6 +144,16 @@ if (_.inBrowser) {
       expect(prevented).toBe(true)
     })
 
+    it('prevent modifier with no value', function () {
+      new Vue({
+        el: el,
+        template: '<a href="#123" @click.prevent>'
+      })
+      var hash = window.location.hash
+      trigger(el.firstChild, 'click')
+      expect(window.location.hash).toBe(hash)
+    })
+
     it('multiple modifiers working together', function () {
       var outer = jasmine.createSpy('outer')
       var prevented