Evan You 12 лет назад
Родитель
Сommit
2232cf2885
5 измененных файлов с 27 добавлено и 5 удалено
  1. 3 1
      examples/repeated-items.html
  2. 5 0
      src/compiler.js
  3. 1 1
      src/directive.js
  4. 12 1
      src/directives/each.js
  5. 6 2
      test/unit/specs/directive.js

+ 3 - 1
examples/repeated-items.html

@@ -19,7 +19,9 @@
             </p>
             <p>Total items: {{items.length}}</p>
             <ul>
-                <li sd-each="item:items" sd-text="item.title"></li>
+                <li sd-each="item:items">
+                    {{item.$index}} {{item.title}}
+                </li>
             </ul>
         </div>
         <script src="../dist/seed.js"></script>

+ 5 - 0
src/compiler.js

@@ -104,6 +104,11 @@ function Compiler (vm, options) {
         }
     }
 
+    // for each items, create an index binding
+    if (this.each) {
+        vm[this.eachPrefix].$index = this.eachIndex
+    }
+
     // now parse the DOM, during which we will create necessary bindings
     // and bind the parsed directives
     this.compileNode(this.el, true)

+ 1 - 1
src/directive.js

@@ -8,7 +8,7 @@ var KEY_RE          = /^[^\|]+/,
     FILTERS_RE      = /\|[^\|]+/g,
     FILTER_TOKEN_RE = /[^\s']+|'[^']+'/g,
     NESTING_RE      = /^\^+/,
-    SINGLE_VAR_RE   = /^[\w\.]+$/
+    SINGLE_VAR_RE   = /^[\w\.\$]+$/
 
 /*
  *  Directive class

+ 12 - 1
src/directives/each.js

@@ -92,7 +92,11 @@ module.exports = {
         this.vms = null
         var self = this
         this.mutationListener = function (path, arr, mutation) {
-            mutationHandlers[mutation.method].call(self, mutation)
+            var method = mutation.method
+            mutationHandlers[method].call(self, mutation)
+            if (method !== 'push' && method !== 'pop') {
+                self.updateIndexes()
+            }
         }
     },
 
@@ -155,6 +159,13 @@ module.exports = {
         }
     },
 
+    updateIndexes: function () {
+        var i = this.vms.length
+        while (i--) {
+            this.vms[i][this.arg].$index = i
+        }
+    },
+
     unbind: function () {
         if (this.collection) {
             this.collection.__observer__.off('mutate', this.mutationListener)

+ 6 - 2
test/unit/specs/directive.js

@@ -97,12 +97,16 @@ describe('UNIT: Directive', function () {
                 e = Directive.parse('sd-text', '!abc', compiler),
                 f = Directive.parse('sd-text', 'abc + bcd * 5 / 2', compiler),
                 g = Directive.parse('sd-text', 'abc && (bcd || eee)', compiler),
-                h = Directive.parse('sd-text', 'test(abc)', compiler)
+                h = Directive.parse('sd-text', 'test(abc)', compiler),
+                i = Directive.parse('sd-text', 'a.b', compiler),
+                j = Directive.parse('sd-text', 'a.$b', compiler)
             assert.ok(!d.isExp, 'non-expression')
             assert.ok(e.isExp, 'negation')
             assert.ok(f.isExp, 'math')
             assert.ok(g.isExp, 'logic')
-            assert.ok(g.isExp, 'function invocation')
+            assert.ok(h.isExp, 'function invocation')
+            assert.ok(!i.isExp, 'dot syntax')
+            assert.ok(!j.isExp, 'dot syntax with $')
         })
 
         it('should have a filter prop of null if no filters are present', function () {