Explorar el Código

parse key path in directive parser

Evan You hace 13 años
padre
commit
c2faf1c143
Se han modificado 4 ficheros con 31 adiciones y 31 borrados
  1. 23 21
      src/directive-parser.js
  2. 1 1
      src/directives/each.js
  3. 6 6
      src/directives/on.js
  4. 1 3
      src/seed.js

+ 23 - 21
src/directive-parser.js

@@ -34,12 +34,9 @@ function Directive (directiveName, expression, oneway) {
     this.oneway        = !!oneway
     this.directiveName = directiveName
     this.expression    = expression.trim()
-    this.rawKey        = expression.match(KEY_RE)[0]
+    this.rawKey        = expression.match(KEY_RE)[0].trim()
     
-    var keyInfo  = parseKey(this.rawKey)
-    for (prop in keyInfo) {
-        this[prop] = keyInfo[prop]
-    }
+    this.parseKey(this.rawKey)
     
     var filterExps = expression.match(FILTERS_RE)
     this.filters = filterExps
@@ -98,38 +95,43 @@ Directive.prototype.applyFilters = function (value) {
 /*
  *  parse a key, extract argument and nesting/root info
  */
-function parseKey (rawKey) {
+Directive.prototype.parseKey = function (rawKey) {
 
-    var res = {},
-        argMatch = rawKey.match(ARG_RE)
+    var argMatch = rawKey.match(ARG_RE)
 
-    res.key = argMatch
+    var key = argMatch
         ? argMatch[2].trim()
         : rawKey.trim()
 
-    res.arg = argMatch
+    this.arg = argMatch
         ? argMatch[1].trim()
         : null
 
-    res.inverse = INVERSE_RE.test(res.key)
-    if (res.inverse) {
-        res.key = res.key.slice(1)
+    this.inverse = INVERSE_RE.test(key)
+    if (this.inverse) {
+        key = key.slice(1)
     }
 
-    var nesting = res.key.match(NESTING_RE)
-    res.nesting = nesting
+    var nesting = key.match(NESTING_RE)
+    this.nesting = nesting
         ? nesting[0].length
         : false
 
-    res.root = res.key.charAt(0) === '$'
+    this.root = key.charAt(0) === '$'
+
+    if (this.nesting) {
+        key = key.replace(NESTING_RE, '')
+    } else if (this.root) {
+        key = key.slice(1)
+    }
 
-    if (res.nesting) {
-        res.key = res.key.replace(NESTING_RE, '')
-    } else if (res.root) {
-        res.key = res.key.slice(1)
+    if (key.indexOf('.') > 0) {
+        var path = key.split('.')
+        key = path[path.length - 1]
+        this.path = path.slice(0, -1)
     }
 
-    return res
+    this.key = key
 }
 
 /*

+ 1 - 1
src/directives/each.js

@@ -109,7 +109,7 @@ module.exports = {
             node = this.el.cloneNode(true)
         var spore = new Seed(node, {
                 each: true,
-                eachPrefixRE: new RegExp('^' + this.arg + '.'),
+                eachPrefix: this.arg,
                 parentSeed: this.seed,
                 index: index,
                 data: data,

+ 6 - 6
src/directives/on.js

@@ -45,9 +45,9 @@ module.exports = {
                 var target = delegateCheck(e.target, delegator, identifier)
                 if (target) {
                     handler({
-                        originalEvent : e,
-                        el            : target,
-                        scope         : target.sd_scope
+                        el: target,
+                        scope: target.sd_scope,
+                        originalEvent: e
                     })
                 }
             }
@@ -59,9 +59,9 @@ module.exports = {
             // a normal, single element handler
             this.handler = function (e) {
                 handler({
-                    originalEvent : e,
-                    el            : e.currentTarget,
-                    scope         : seed.scope
+                    el: e.currentTarget,
+                    scope: seed.scope,
+                    originalEvent: e
                 })
             }
             this.el.addEventListener(event, this.handler)

+ 1 - 3
src/seed.js

@@ -170,9 +170,7 @@ Seed.prototype._bind = function (directive) {
         seed = directive.seed = this
 
     if (this.each) {
-        if (this.eachPrefixRE.test(key)) {
-            key = directive.key = key.replace(this.eachPrefixRE, '')
-        } else {
+        if (!directive.path || directive.path[0] !== this.eachPrefix) {
             seed = this.parentSeed
         }
     }