Browse Source

prefix internal directive methods with $ so its less confusing

Evan You 12 years ago
parent
commit
6be817eebe

+ 2 - 2
src/binding.js

@@ -52,7 +52,7 @@ BindingProto._update = function () {
     var i = this.dirs.length,
         value = this.val()
     while (i--) {
-        this.dirs[i].update(value)
+        this.dirs[i].$update(value)
     }
     this.pub()
 }
@@ -89,7 +89,7 @@ BindingProto.unbind = function () {
     this.unbound = true
     var i = this.dirs.length
     while (i--) {
-        this.dirs[i].unbind()
+        this.dirs[i].$unbind()
     }
     i = this.deps.length
     var subs

+ 2 - 2
src/compiler.js

@@ -681,7 +681,7 @@ CompilerProto.bindDirective = function (directive, bindingOwner) {
         directive.bind(value)
     }
     // set initial value
-    directive.update(value, true)
+    directive.$update(value, true)
 }
 
 /**
@@ -955,7 +955,7 @@ CompilerProto.destroy = function () {
                 if (j > -1) dirs.splice(j, 1)
             }
         }
-        dir.unbind()
+        dir.$unbind()
     }
 
     // unbind all computed, anonymous bindings

+ 10 - 13
src/directive.js

@@ -25,14 +25,10 @@ function Directive (name, ast, definition, compiler, el) {
 
     // mix in properties from the directive definition
     if (typeof definition === 'function') {
-        this[isEmpty ? 'bind' : '_update'] = definition
+        this[isEmpty ? 'bind' : 'update'] = definition
     } else {
         for (var prop in definition) {
-            if (prop === 'unbind' || prop === 'update') {
-                this['_' + prop] = definition[prop]
-            } else {
-                this[prop] = definition[prop]
-            }
+            this[prop] = definition[prop]
         }
     }
 
@@ -88,13 +84,14 @@ var DirProto = Directive.prototype
  *  for computed properties, this will only be called once
  *  during initialization.
  */
-DirProto.update = function (value, init) {
+DirProto.$update = function (value, init) {
+    if (this.$lock) return
     if (init || value !== this.value || (value && typeof value === 'object')) {
         this.value = value
-        if (this._update) {
-            this._update(
+        if (this.update) {
+            this.update(
                 this.filters && !this.computeFilters
-                    ? this.applyFilters(value)
+                    ? this.$applyFilters(value)
                     : value,
                 init
             )
@@ -105,7 +102,7 @@ DirProto.update = function (value, init) {
 /**
  *  pipe the value through filters
  */
-DirProto.applyFilters = function (value) {
+DirProto.$applyFilters = function (value) {
     var filtered = value, filter
     for (var i = 0, l = this.filters.length; i < l; i++) {
         filter = this.filters[i]
@@ -117,10 +114,10 @@ DirProto.applyFilters = function (value) {
 /**
  *  Unbind diretive
  */
-DirProto.unbind = function () {
+DirProto.$unbind = function () {
     // this can be called before the el is even assigned...
     if (!this.el || !this.vm) return
-    if (this._unbind) this._unbind()
+    if (this.unbind) this.unbind()
     this.vm = this.el = this.binding = this.compiler = null
 }
 

+ 1 - 1
src/directives/if.js

@@ -32,7 +32,7 @@ module.exports = {
     update: function (value) {
 
         if (!value) {
-            this._unbind()
+            this.unbind()
         } else if (!this.childVM) {
             this.childVM = new this.Ctor({
                 el: this.el.cloneNode(true),

+ 1 - 1
src/directives/on.js

@@ -18,7 +18,7 @@ module.exports = {
             utils.warn('Directive "v-on:' + this.expression + '" expects a method.')
             return
         }
-        this._unbind()
+        this.unbind()
         var vm = this.vm,
             context = this.context
         this.handler = function (e) {

+ 1 - 1
src/directives/view.js

@@ -25,7 +25,7 @@ module.exports = {
 
     update: function(value) {
 
-        this._unbind()
+        this.unbind()
 
         var Ctor  = this.compiler.getOption('components', value)
         if (!Ctor) return

+ 2 - 2
test/unit/specs/binding.js

@@ -37,7 +37,7 @@ describe('Binding', function () {
             pubbed = false,
             numInstances = 3,
             instance = {
-                update: function (value) {
+                $update: function (value) {
                     updated += value
                 }
             }
@@ -135,7 +135,7 @@ describe('Binding', function () {
             unbound = 0,
             numInstances = 3,
             instance = {
-                unbind: function () {
+                $unbind: function () {
                     unbound++
                 }
             }

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

@@ -148,13 +148,13 @@ describe('Directive', function () {
 
     describe('instantiation', function () {
         
-        it('should copy the definition as _update if the def is a function', function () {
+        it('should copy the definition as update if the def is a function', function () {
 
             var testDir = function () {}
             directives.test = testDir
 
             var d = build('test', 'abc', compiler)
-            assert.strictEqual(d._update, testDir)
+            assert.strictEqual(d.update, testDir)
         })
 
         it('should copy methods if the def is an object', function () {
@@ -168,8 +168,8 @@ describe('Directive', function () {
             directives.obj = obj
             
             var d = build('obj', 'abc', compiler)
-            assert.strictEqual(d._update, obj.update, 'update should be copied as _update')
-            assert.strictEqual(d._unbind, obj.unbind, 'unbind should be copied as _unbind')
+            assert.strictEqual(d.update, obj.update)
+            assert.strictEqual(d.unbind, obj.unbind)
             assert.strictEqual(d.bind, obj.bind)
             assert.strictEqual(d.custom, obj.custom, 'should copy any custom methods')
         })
@@ -261,80 +261,80 @@ describe('Directive', function () {
 
     })
 
-    describe('.applyFilters()', function () {
+    describe('.$applyFilters()', function () {
         
         it('should work', function () {
             var d = build('text', 'abc | pluralize item | capitalize', compiler),
-                v = d.applyFilters(2)
+                v = d.$applyFilters(2)
             assert.strictEqual(v, 'Items')
         })
 
     })
 
-    describe('.update()', function () {
+    describe('.$update()', function () {
         
         var d = build('text', 'abc', compiler),
             updated = false
-        d._update = function () {
+        d.update = function () {
             updated = true
         }
 
-        it('should call _update() for first time update, even with undefined', function () {
-            d.update(undefined, true)
+        it('should call user update() for first time update, even with undefined', function () {
+            d.$update(undefined, true)
             assert.strictEqual(updated, true)
         })
 
-        it('should _update() when a different value is given', function () {
+        it('should user update() when a different value is given', function () {
             updated = false
-            d.update(123)
+            d.$update(123)
             assert.strictEqual(d.value, 123)
             assert.strictEqual(updated, true)
         })
 
-        it('should not _update() if the value is the same', function () {
+        it('should not call user update() if the value is the same', function () {
             updated = false
-            d.update(123)
+            d.$update(123)
             assert.ok(!updated)
         })
 
-        it('should call applyFilter() is there are filters', function () {
+        it('should call $applyFilter() is there are filters', function () {
             var filterApplied = false
             d.filters = []
-            d.applyFilters = function () {
+            d.$applyFilters = function () {
                 filterApplied = true
             }
-            d.update(234)
+            d.$update(234)
             assert.ok(filterApplied)
         })
 
     })
 
-    describe('.unbind()', function () {
+    describe('.$unbind()', function () {
         
         var d = build('text', 'abc', compiler),
             unbound = false,
             val
-        d._unbind = function (v) {
+        d.unbind = function (v) {
             val = v
             unbound = true
         }
 
         it('should not work if it has no element yet', function () {
             d.el = null
-            d.unbind()
+            d.$unbind()
             assert.strictEqual(unbound, false)
         })
 
-        it('should call _unbind() and null everything if it has an element', function () {
+        it('should call user unbind() and null everything if it has an element', function () {
             d.el = true
-            d.unbind()
+            d.$unbind()
             assert.strictEqual(unbound, true)
             assert.ok(d.el === null && d.vm === null && d.binding === null && d.compiler === null)
         })
 
         it('should not execute if called more than once', function () {
             unbound = false
-            d.unbind()
+            d.$unbind()
             assert.notOk(unbound)
         })
 

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

@@ -417,7 +417,7 @@ describe('ViewModel', function () {
                 compiler: null,
                 dirs: []
             },
-            unbind: function () {
+            $unbind: function () {
                 dirUnbindCalled = true
             }
         }