Преглед на файлове

move $index to VM instead of item, add $parent & $collection

Evan You преди 12 години
родител
ревизия
db284ffa94

+ 14 - 10
src/compiler.js

@@ -11,7 +11,6 @@ var Emitter     = require('./emitter'),
     // cache methods
     slice       = Array.prototype.slice,
     log         = utils.log,
-    def         = utils.defProtected,
     makeHash    = utils.hash,
     hasOwn      = Object.prototype.hasOwnProperty,
 
@@ -48,10 +47,9 @@ function Compiler (vm, options) {
     if (scope) utils.extend(vm, scope, true)
 
     compiler.vm  = vm
-    // special VM properties are inumerable
-    def(vm, '$', makeHash())
-    def(vm, '$el', compiler.el)
-    def(vm, '$compiler', compiler)
+    vm.$ = makeHash()
+    vm.$el = compiler.el
+    vm.$compiler = compiler
 
     // keep track of directives and expressions
     // so they can be unbound during destroy()
@@ -75,11 +73,15 @@ function Compiler (vm, options) {
         ? getRoot(parent)
         : compiler
 
-    // register child id on parent
+    // set parent VM
+    // and register child id on parent
     var childId = compiler.el.getAttribute(idAttr)
-    if (childId && parent) {
-        compiler.childId = childId
-        parent.vm.$[childId] = vm
+    if (parent) {
+        vm.$parent = parent.vm
+        if (childId) {
+            compiler.childId = childId
+            parent.vm.$[childId] = vm
+        }
     }
 
     // setup observer
@@ -102,7 +104,9 @@ function Compiler (vm, options) {
     // for repeated items, create an index binding
     // which should be inenumerable but configurable
     if (compiler.repeat) {
-        def(vm[compiler.repeatPrefix], '$index', compiler.repeatIndex, false, true)
+        vm.$index = compiler.repeatIndex
+        vm.$collection = compiler.repeatCollection
+        compiler.createBinding('$index')
     }
 
     // now parse the DOM, during which we will create necessary bindings

+ 1 - 1
src/directive.js

@@ -85,7 +85,7 @@ function parseKey (dir, rawKey) {
         ? nesting[0].length
         : false
 
-    dir.root = key.charAt(0) === '$'
+    dir.root = key.charAt(0) === '*'
 
     if (dir.nesting) {
         key = key.replace(NESTING_RE, '')

+ 2 - 1
src/directives/repeat.js

@@ -168,6 +168,7 @@ module.exports = {
             compilerOptions: {
                 repeat: true,
                 repeatIndex: index,
+                repeatCollection: this.collection,
                 repeatPrefix: this.arg,
                 parentCompiler: this.compiler,
                 delegator: ctn
@@ -189,7 +190,7 @@ module.exports = {
     updateIndexes: function () {
         var i = this.vms.length
         while (i--) {
-            this.vms[i][this.arg].$index = i
+            this.vms[i].$index = i
         }
     },
 

+ 0 - 4
src/observer.js

@@ -79,10 +79,6 @@ function watchObject (obj, path, observer) {
             bind(obj, key, path, observer)
         }
     }
-    // $index is inenumerable
-    if (obj.$index !== undefined) {
-        bind(obj, '$index', path, observer)
-    }
 }
 
 /**

+ 1 - 1
test/functional/fixtures/nested-viewmodels.html

@@ -47,7 +47,7 @@
     </div>
 
     <script type="text/sd-template" id="sd-template-offspring">
-        <p>{{name}}, son of {{^name}}, grandson of {{^^name}}, great-grandson of {{$name}}, and offspring of family {{family}}.</p>
+        <p>{{name}}, son of {{^name}}, grandson of {{^^name}}, great-grandson of {{*name}}, and offspring of family {{family}}.</p>
     </script>
     
     <script>

+ 1 - 1
test/functional/fixtures/repeated-items.html

@@ -22,7 +22,7 @@
             <p>Total items: <span class="count" sd-text="items.length"></span></p>
             <ul>
                 <li class="item" sd-repeat="item:items">
-                    {{item.$index}} {{item.title}}
+                    {{$index}} {{item.title}}
                 </li>
             </ul>
         </div>

+ 1 - 1
test/unit/specs/directive.js

@@ -90,7 +90,7 @@ describe('UNIT: Directive', function () {
             var d = Directive.parse('sd-text', 'abc', compiler),
                 e = Directive.parse('sd-text', '^abc', compiler),
                 f = Directive.parse('sd-text', '^^^abc', compiler),
-                g = Directive.parse('sd-text', '$abc', compiler)
+                g = Directive.parse('sd-text', '*abc', compiler)
             assert.ok(d.nesting === false && d.root === false && d.key === 'abc', 'no nesting')
             assert.ok(e.nesting === 1 && e.root === false && e.key === 'abc', '1 level')
             assert.ok(f.nesting === 3 && f.root === false && f.key === 'abc', '3 levels')