Преглед изворни кода

add isLiteral option for custom directive

Evan You пре 12 година
родитељ
комит
6673828542
3 измењених фајлова са 25 додато и 3 уклоњено
  1. 2 2
      src/compiler.js
  2. 7 0
      src/directive.js
  3. 16 1
      test/unit/specs/api.js

+ 2 - 2
src/compiler.js

@@ -433,9 +433,9 @@ CompilerProto.bindDirective = function (directive) {
     // keep track of it so we can unbind() later
     this.dirs.push(directive)
 
-    // for a simple directive, simply call its bind() or _update()
+    // for empty or literal directives, simply call its bind()
     // and we're done.
-    if (directive.isEmpty) {
+    if (directive.isEmpty || directive.isLiteral) {
         if (directive.bind) directive.bind()
         return
     }

+ 7 - 0
src/directive.js

@@ -48,6 +48,13 @@ function Directive (definition, expression, rawKey, compiler, node) {
         return
     }
 
+    // for literal directives, all we need
+    // is the expression as the value.
+    if (this.isLiteral) {
+        this.value = expression.trim()
+        return
+    }
+
     this.expression = expression.trim()
     this.rawKey     = rawKey
     

+ 16 - 1
test/unit/specs/api.js

@@ -66,7 +66,7 @@ describe('UNIT: API', function () {
 
         var dirTest
         
-        it('should create custom directive with set function only', function () {
+        it('should create custom directive with update() function only', function () {
             var testId = 'directive-1',
                 msg = 'wowow'
             Vue.directive('test', function (value) {
@@ -108,6 +108,21 @@ describe('UNIT: API', function () {
             assert.notOk(el.getAttribute(testId + 'bind'), 'should have called unbind()')
         })
 
+        it('should create literal directive if given option', function () {
+            var called = false
+            Vue.directive('test-literal', {
+                isLiteral: true,
+                bind: function () {
+                    called = true
+                    assert.strictEqual(this.value, 'hihi')
+                }
+            })
+            new Vue({
+                template: '<div v-test-literal="hihi"></div>'
+            })
+            assert.ok(called)
+        })
+
         it('should return directive object/fn if only one arg is given', function () {
             var dir = Vue.directive('test2')
             assert.strictEqual(dir, dirTest)