Browse Source

tests for sd-id

Evan You 12 years ago
parent
commit
fa2907372c
7 changed files with 35 additions and 11 deletions
  1. 1 1
      src/compiler.js
  2. 1 1
      src/directives/repeat.js
  3. 2 2
      src/main.js
  4. 1 1
      src/utils.js
  5. 3 3
      test/unit/specs/api.js
  6. 24 0
      test/unit/specs/directives.js
  7. 3 3
      test/unit/specs/viewmodel.js

+ 1 - 1
src/compiler.js

@@ -209,7 +209,7 @@ CompilerProto.compile = function (node, root) {
             }
         } else if (vmId && !root) { // child ViewModels
             node.removeAttribute(vmAttr)
-            var ChildVM = compiler.getOption('vms', vmId)
+            var ChildVM = compiler.getOption('viewmodels', vmId)
             if (ChildVM) {
                 var child = new ChildVM({
                     el: node,

+ 1 - 1
src/directives/repeat.js

@@ -140,7 +140,7 @@ module.exports = {
         var node = this.el.cloneNode(true),
             ctn  = this.container,
             vmID = node.getAttribute(config.prefix + '-viewmodel'),
-            ChildVM = this.compiler.getOption('vms', vmID) || ViewModel,
+            ChildVM = this.compiler.getOption('viewmodels', vmID) || ViewModel,
             scope = {}
         scope[this.arg] = data || {}
         var item = new ChildVM({

+ 2 - 2
src/main.js

@@ -36,8 +36,8 @@ ViewModel.filter = function (id, fn) {
  *  Allows user to register/retrieve a ViewModel constructor
  */
 ViewModel.viewmodel = function (id, Ctor) {
-    if (!Ctor) return utils.vms[id]
-    utils.vms[id] = Ctor
+    if (!Ctor) return utils.viewmodels[id]
+    utils.viewmodels[id] = Ctor
     return this
 }
 

+ 1 - 1
src/utils.js

@@ -7,7 +7,7 @@ module.exports = {
 
     // global storage for user-registered
     // vms, partials and transitions
-    vms         : {},
+    viewmodels  : {},
     partials    : {},
     transitions : {},
 

+ 3 - 3
test/unit/specs/api.js

@@ -112,7 +112,7 @@ describe('UNIT: API', function () {
 
         it('should register a VM constructor', function () {
             Seed.viewmodel(testId, Test)
-            assert.strictEqual(utils.vms[testId], Test)
+            assert.strictEqual(utils.viewmodels[testId], Test)
         })
 
         it('should retrieve the VM if has only one arg', function () {
@@ -454,7 +454,7 @@ describe('UNIT: API', function () {
 
             })
 
-            describe('vms', function () {
+            describe('viewmodels', function () {
 
                 it('should allow the VM to use private child VMs', function () {
                     var Child = Seed.extend({
@@ -467,7 +467,7 @@ describe('UNIT: API', function () {
                         scope: {
                             name: 'dad'
                         },
-                        vms: {
+                        viewmodels: {
                             child: Child
                         }
                     })

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

@@ -546,6 +546,30 @@ describe('UNIT: Directives', function () {
 
     })
 
+    describe('id', function () {
+        
+        it('should register a VM isntance on its parent\'s $', function () {
+            var called = false
+            var Child = Seed.extend({
+                proto: {
+                    test: function () {
+                        called = true
+                    }
+                }
+            })
+            var t = new Seed({
+                template: '<div sd-viewmodel="child" sd-id="hihi"></div>',
+                viewmodels: {
+                    child: Child
+                }
+            })
+            assert.ok(t.$.hihi instanceof Child)
+            t.$.hihi.test()
+            assert.ok(called)
+        })
+
+    })
+
 })
 
 function mockDirective (dirName, tag, type) {

+ 3 - 3
test/unit/specs/viewmodel.js

@@ -174,7 +174,7 @@ describe('UNIT: ViewModel', function () {
             })
             var Test = Seed.extend({
                 template: '<div sd-viewmodel="test"></div><div sd-viewmodel="test"></div>',
-                vms: {
+                viewmodels: {
                     test: Child
                 }
             })
@@ -204,7 +204,7 @@ describe('UNIT: ViewModel', function () {
             })
             var Middle = Seed.extend({
                 template: '<div sd-viewmodel="bottom"></div>',
-                vms: { bottom: Bottom },
+                viewmodels: { bottom: Bottom },
                 init: function () {
                     this.$on('hello', function (m) {
                         assert.strictEqual(m, msg)
@@ -214,7 +214,7 @@ describe('UNIT: ViewModel', function () {
             })
             var Top = Seed.extend({
                 template: '<div sd-viewmodel="middle"></div>',
-                vms: { middle: Middle },
+                viewmodels: { middle: Middle },
                 init: function () {
                     this.$on('hello', function (m) {
                         assert.strictEqual(m, msg)