Przeglądaj źródła

dataAttributes options (#125)

Evan You 12 lat temu
rodzic
commit
659593f0e6
4 zmienionych plików z 26 dodań i 30 usunięć
  1. 8 0
      src/compiler.js
  2. 1 15
      src/directives/index.js
  3. 17 0
      test/unit/specs/api.js
  4. 0 15
      test/unit/specs/directives.js

+ 8 - 0
src/compiler.js

@@ -89,6 +89,14 @@ function Compiler (vm, options) {
         }
     }
 
+    // copy paramAttributes
+    if (options.paramAttributes) {
+        options.paramAttributes.forEach(function (attr) {
+            var val = el.getAttribute(attr)
+            vm[attr] = isNaN(val) ? val : Number(val)
+        })
+    }
+
     // beforeCompile hook
     compiler.execHook('created')
 

+ 1 - 15
src/directives/index.js

@@ -1,8 +1,6 @@
 var utils      = require('../utils'),
     config     = require('../config'),
-    transition = require('../transition'),
-    NumberRE   = /^[\d\.]+$/,
-    CommaRE    = /\\,/g
+    transition = require('../transition')
 
 module.exports = {
 
@@ -56,18 +54,6 @@ module.exports = {
                 el.removeAttribute(config.prefix + '-cloak')
             })
         }
-    },
-
-    data: {
-        bind: function () {
-            var val = this.key
-            this.vm.$set(
-                this.arg,
-                NumberRE.test(val)
-                    ? +val
-                    : val.replace(CommaRE, ',')
-            )
-        }
     }
 
 }

+ 17 - 0
test/unit/specs/api.js

@@ -602,6 +602,23 @@ describe('UNIT: API', function () {
 
             })
 
+            describe('paramAttributes', function () {
+                
+                it('should copy listed attributes into data and parse Numbers', function () {
+                    var Test = Vue.extend({
+                        template: '<div a="1" b="hello"></div>',
+                        replace: true,
+                        paramAttributes: ['a', 'b']
+                    })
+                    var v = new Test()
+                    assert.strictEqual(v.a, 1)
+                    assert.strictEqual(v.$data.a, 1)
+                    assert.strictEqual(v.b, 'hello')
+                    assert.strictEqual(v.$data.b, 'hello')
+                })
+
+            })
+
             describe('directives', function () {
                 
                 it('should allow the VM to use private directives', function (done) {

+ 0 - 15
test/unit/specs/directives.js

@@ -844,21 +844,6 @@ describe('UNIT: Directives', function () {
 
     })
 
-    describe('data', function () {
-        
-        it('should set data on the child VM', function () {
-            var v = new Vue({
-                template: '<div v-component="test" v-ref="test" v-data="a:1,b:hi"></div>',
-                components: {
-                    test: Vue
-                }
-            })
-            assert.strictEqual(v.$.test.a, 1)
-            assert.strictEqual(v.$.test.b, 'hi')
-        })
-
-    })
-
 })
 
 function mockDirective (dirName, tag, type) {