Browse Source

child VMs should remove itself from parent's $ when destroyed

Evan You 12 years ago
parent
commit
be72b739c5
3 changed files with 8 additions and 33 deletions
  1. 6 1
      src/compiler.js
  2. 0 32
      test/functional/fixtures/child-id.html
  3. 2 0
      test/unit/specs/directives.js

+ 6 - 1
src/compiler.js

@@ -78,6 +78,7 @@ function Compiler (vm, options) {
     // register child id on parent
     var childId = compiler.el.getAttribute(idAttr)
     if (childId && parent) {
+        compiler.childId = childId
         parent.vm.$[childId] = vm
     }
 
@@ -590,9 +591,13 @@ CompilerProto.destroy = function () {
         }
     }
     // remove self from parentCompiler
-    var parent = compiler.parentCompiler
+    var parent = compiler.parentCompiler,
+        childId = compiler.childId
     if (parent) {
         parent.childCompilers.splice(parent.childCompilers.indexOf(compiler), 1)
+        if (childId) {
+            delete parent.vm.$[childId]
+        }
     }
     // remove el
     if (el === document.body) {

+ 0 - 32
test/functional/fixtures/child-id.html

@@ -1,32 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-    <head>
-        <title></title>
-        <meta charset="utf-8">
-        <script src="../../../dist/seed.js"></script>
-    </head>
-    <body>
-        <div id="parent">
-            <div sd-id="child" sd-viewmodel="child">
-                {{msg}}
-            </div>
-        </div>
-        <script>
-            Seed.viewmodel('child', Seed.extend({
-                init: function () {
-                    console.log('child init!')
-                },
-                proto: {
-                    hi: function () {
-                        console.log('hi from child!')
-                    }
-                },
-                scope: {
-                    msg: 'I am a child'
-                }
-            }))
-            var app = new Seed({el:'#parent'})
-            app.$.child.msg = 'Set from the parent!'
-        </script>
-    </body>
-</html>

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

@@ -566,6 +566,8 @@ describe('UNIT: Directives', function () {
             assert.ok(t.$.hihi instanceof Child)
             t.$.hihi.test()
             assert.ok(called)
+            t.$.hihi.$destroy()
+            assert.notOk('hihi' in t.$)
         })
 
     })