Przeglądaj źródła

fix nested VM example

Evan You 12 lat temu
rodzic
commit
55ec2fca9c
3 zmienionych plików z 15 dodań i 11 usunięć
  1. 9 6
      examples/nested-viewmodels.html
  2. 4 4
      src/compiler.js
  3. 2 1
      src/directives/each.js

+ 9 - 6
examples/nested-viewmodels.html

@@ -19,7 +19,7 @@
     </style>
 </head>
 <body>
-    <div id="grandpa" sd-viewmodel="man" data-name="Andy" data-family="Johnson">
+    <div id="grandpa" data-name="Andy" data-family="Johnson">
         <p class="ancestor">{{name}} {{family}}</p>
 
         <div sd-viewmodel="man" data-name="Jack">
@@ -58,10 +58,8 @@
     <script src="../dist/seed.js"></script>
     <script>
         seed.config({ debug: true })
-        seed.template('offspring', document.getElementById('sd-template-offspring').innerHTML)
 
         var Man = seed.ViewModel.extend({
-            id: 'man',
             init: function () {
                 this.name = this.$el.dataset.name
                 var family = this.$el.dataset.family
@@ -72,11 +70,16 @@
         })
 
         var Offspring = Man.extend({
-            id: 'offspring',
-            template: 'offspring'
+            template: document.getElementById('sd-template-offspring').innerHTML
         })
 
-        seed.compile('#grandpa')
+        seed.vm('man', Man)
+        seed.vm('offspring', Offspring)
+
+        new Man({
+            el: '#grandpa'
+        })
+        
     </script>
 </body>
 </html>

+ 4 - 4
src/compiler.js

@@ -22,9 +22,7 @@ function Compiler (vm, options) {
     vmAttr   = config.prefix + '-viewmodel'
 
     options = this.options = options || {}
-    if (options.eachOptions) {
-        utils.extend(this, options.eachOptions)
-    }
+    utils.extend(this, options.compilerOptions || {})
 
     // initialize element
     var el  = typeof options.el === 'string'
@@ -193,7 +191,9 @@ CompilerProto.compileNode = function (node, root) {
                 new ChildVM({
                     el: node,
                     child: true,
-                    parentCompiler: compiler
+                    compilerOptions: {
+                        parentCompiler: compiler
+                    }
                 })
             }
 

+ 2 - 1
src/directives/each.js

@@ -136,8 +136,9 @@ module.exports = {
         var item = new ChildVM({
             el: node,
             data: wrappedData,
-            eachOptions: {
+            compilerOptions: {
                 each: true,
+                eachIndex: index,
                 eachPrefix: this.arg,
                 parentCompiler: this.compiler,
                 delegator: ctn