瀏覽代碼

remove unecessary nulling, change comment style

Evan You 12 年之前
父節點
當前提交
2473d78706
共有 14 個文件被更改,包括 101 次插入108 次删除
  1. 5 6
      src/binding.js
  2. 21 21
      src/compiler.js
  3. 3 3
      src/deps-parser.js
  4. 10 10
      src/directive.js
  5. 1 1
      src/directives/index.js
  6. 4 4
      src/directives/repeat.js
  7. 1 1
      src/exp-parser.js
  8. 1 1
      src/filters.js
  9. 10 10
      src/main.js
  10. 8 8
      src/observer.js
  11. 1 1
      src/text-parser.js
  12. 6 6
      src/utils.js
  13. 30 28
      src/viewmodel.js
  14. 0 8
      test/unit/specs/binding.js

+ 5 - 6
src/binding.js

@@ -1,4 +1,4 @@
-/*
+/**
  *  Binding class.
  *
  *  each property on the viewmodel has one corresponding Binding object
@@ -18,7 +18,7 @@ function Binding (compiler, key, isExp) {
 
 var BindingProto = Binding.prototype
 
-/*
+/**
  *  Process the value, then trigger updates on all dependents
  */
 BindingProto.update = function (value) {
@@ -30,7 +30,7 @@ BindingProto.update = function (value) {
     this.pub()
 }
 
-/*
+/**
  *  -- computed property only --    
  *  Force all instances to re-evaluate themselves
  */
@@ -42,7 +42,7 @@ BindingProto.refresh = function () {
     this.pub()
 }
 
-/*
+/**
  *  Notify computed properties that depend on this binding
  *  to update themselves
  */
@@ -53,7 +53,7 @@ BindingProto.pub = function () {
     }
 }
 
-/*
+/**
  *  Unbind the binding, remove itself from all of its dependencies
  */
 BindingProto.unbind = function () {
@@ -67,7 +67,6 @@ BindingProto.unbind = function () {
         subs = this.deps[i].subs
         subs.splice(subs.indexOf(this), 1)
     }
-    this.compiler = this.pubs = this.subs = this.instances = this.deps = null
 }
 
 module.exports = Binding

+ 21 - 21
src/compiler.js

@@ -9,13 +9,14 @@ var Emitter     = require('./emitter'),
     ExpParser   = require('./exp-parser'),
     slice       = Array.prototype.slice,
     log         = utils.log,
+    def         = utils.defProtected,
     vmAttr,
     repeatAttr,
     partialAttr,
     transitionAttr,
     preAttr
 
-/*
+/**
  *  The DOM compiler
  *  scans a DOM node and compile bindings for a ViewModel
  */
@@ -38,8 +39,9 @@ function Compiler (vm, options) {
     if (scope) utils.extend(vm, scope, true)
 
     compiler.vm  = vm
-    vm.$compiler = compiler
-    vm.$el       = compiler.el
+    // special VM properties are inumerable
+    def(vm, '$compiler', compiler)
+    def(vm, '$el', compiler.el)
 
     // keep track of directives and expressions
     // so they can be unbound during destroy()
@@ -99,13 +101,11 @@ function Compiler (vm, options) {
     }
     // extract dependencies for computed properties
     if (computed.length) DepsParser.parse(computed)
-    // unset these no longer needed stuff
-    compiler.observables = compiler.computed = compiler.arrays = null
 }
 
 var CompilerProto = Compiler.prototype
 
-/*
+/**
  *  Initialize the VM/Compiler's element.
  *  Fill it in with the template if necessary.
  */
@@ -142,7 +142,7 @@ CompilerProto.setupElement = function (options) {
     }
 }
 
-/*
+/**
  *  Setup observer.
  *  The observer listens for get/set/mutate events on all VM
  *  values/objects and trigger corresponding binding updates.
@@ -174,7 +174,7 @@ CompilerProto.setupObserver = function () {
         })
 }
 
-/*
+/**
  *  Compile a DOM node (recursive)
  */
 CompilerProto.compile = function (node, root) {
@@ -222,7 +222,7 @@ CompilerProto.compile = function (node, root) {
     }
 }
 
-/*
+/**
  *  Compile a normal node
  */
 CompilerProto.compileNode = function (node) {
@@ -260,7 +260,7 @@ CompilerProto.compileNode = function (node) {
     }
 }
 
-/*
+/**
  *  Compile a text node
  */
 CompilerProto.compileTextNode = function (node) {
@@ -293,7 +293,7 @@ CompilerProto.compileTextNode = function (node) {
     node.parentNode.removeChild(node)
 }
 
-/*
+/**
  *  Add a directive instance to the correct binding & viewmodel
  */
 CompilerProto.bindDirective = function (directive) {
@@ -349,7 +349,7 @@ CompilerProto.bindDirective = function (directive) {
     }
 }
 
-/*
+/**
  *  Create binding and attach getter/setter for a key to the viewmodel object
  */
 CompilerProto.createBinding = function (key, isExp) {
@@ -400,7 +400,7 @@ CompilerProto.createBinding = function (key, isExp) {
     return binding
 }
 
-/*
+/**
  *  Sometimes when a binding is found in the template, the value might
  *  have not been set on the VM yet. To ensure computed properties and
  *  dependency extraction can work, we have to create a dummy value for
@@ -419,7 +419,7 @@ CompilerProto.ensurePath = function (key) {
     }
 }
 
-/*
+/**
  *  Defines the getter/setter for a root-level binding on the VM
  *  and observe the initial value
  */
@@ -477,7 +477,7 @@ CompilerProto.define = function (key, binding) {
     })
 }
 
-/*
+/**
  *  Process a computed property binding
  */
 CompilerProto.markComputed = function (binding) {
@@ -491,7 +491,7 @@ CompilerProto.markComputed = function (binding) {
     this.computed.push(binding)
 }
 
-/*
+/**
  *  Process subscriptions for computed properties that has
  *  dynamic context dependencies
  */
@@ -512,7 +512,7 @@ CompilerProto.bindContexts = function (bindings) {
     }
 }
 
-/*
+/**
  *  Retrive an option from the compiler
  */
 CompilerProto.getOption = function (type, id) {
@@ -520,7 +520,7 @@ CompilerProto.getOption = function (type, id) {
     return (opts[type] && opts[type][id]) || (utils[type] && utils[type][id])
 }
 
-/*
+/**
  *  Unbind and remove element
  */
 CompilerProto.destroy = function () {
@@ -574,7 +574,7 @@ CompilerProto.destroy = function () {
 
 // Helpers --------------------------------------------------------------------
 
-/*
+/**
  *  Refresh prefix in case it has been changed
  *  during compilations
  */
@@ -587,7 +587,7 @@ function refreshPrefix () {
     preAttr        = prefix + '-pre'
 }
 
-/*
+/**
  *  determine which viewmodel a key belongs to based on nesting symbols
  */
 function traceOwnerCompiler (key, compiler) {
@@ -604,7 +604,7 @@ function traceOwnerCompiler (key, compiler) {
     return compiler
 }
 
-/*
+/**
  *  shorthand for getting root compiler
  */
 function getRoot (compiler) {

+ 3 - 3
src/deps-parser.js

@@ -2,7 +2,7 @@ var Emitter  = require('./emitter'),
     utils    = require('./utils'),
     observer = new Emitter()
 
-/*
+/**
  *  Auto-extract the dependencies of a computed property
  *  by recording the getters triggered when evaluating it.
  */
@@ -22,12 +22,12 @@ function catchDeps (binding) {
 
 module.exports = {
 
-    /*
+    /**
      *  the observer that catches events triggered by getters
      */
     observer: observer,
 
-    /*
+    /**
      *  parse a list of computed property bindings
      */
     parse: function (bindings) {

+ 10 - 10
src/directive.js

@@ -10,7 +10,7 @@ var KEY_RE          = /^[^\|]+/,
     NESTING_RE      = /^\^+/,
     SINGLE_VAR_RE   = /^[\w\.\$]+$/
 
-/*
+/**
  *  Directive class
  *  represents a single directive instance in the DOM
  */
@@ -57,7 +57,7 @@ function Directive (definition, directiveName, expression, rawKey, compiler, nod
 
 var DirProto = Directive.prototype
 
-/*
+/**
  *  parse a key, extract argument and nesting/root info
  */
 function parseKey (dir, rawKey) {
@@ -88,7 +88,7 @@ function parseKey (dir, rawKey) {
     dir.key = key
 }
 
-/*
+/**
  *  parse a filter expression
  */
 function parseFilter (filter, compiler) {
@@ -115,7 +115,7 @@ function parseFilter (filter, compiler) {
     }
 }
 
-/*
+/**
  *  called when a new value is set 
  *  for computed properties, this will only be called once
  *  during initialization.
@@ -126,7 +126,7 @@ DirProto.update = function (value, init) {
     this.apply(value)
 }
 
-/*
+/**
  *  -- computed property only --
  *  called when a dependency has changed
  */
@@ -143,7 +143,7 @@ DirProto.refresh = function (value) {
     this.apply(value)
 }
 
-/*
+/**
  *  Actually invoking the _update from the directive's definition
  */
 DirProto.apply = function (value) {
@@ -154,7 +154,7 @@ DirProto.apply = function (value) {
     )
 }
 
-/*
+/**
  *  pipe the value through filters
  */
 DirProto.applyFilters = function (value) {
@@ -166,11 +166,11 @@ DirProto.applyFilters = function (value) {
     return filtered
 }
 
-/*
+/**
  *  Unbind diretive
  *  @ param {Boolean} update
  *    Sometimes we call unbind before an update (i.e. not destroy)
- *    just to teardown previousstuff, in that case we do not want
+ *    just to teardown previous stuff, in that case we do not want
  *    to null everything.
  */
 DirProto.unbind = function (update) {
@@ -180,7 +180,7 @@ DirProto.unbind = function (update) {
     if (!update) this.vm = this.el = this.binding = this.compiler = null
 }
 
-/*
+/**
  *  make sure the directive and expression is valid
  *  before we create an instance
  */

+ 1 - 1
src/directives/index.js

@@ -81,7 +81,7 @@ module.exports = {
     }
 }
 
-/*
+/**
  *  convert hyphen style CSS property to Camel style
  */
 var CONVERT_RE = /-(.)/g

+ 4 - 4
src/directives/repeat.js

@@ -3,7 +3,7 @@ var config   = require('../config'),
     Emitter  = require('../emitter'),
     ViewModel // lazy def to avoid circular dependency
 
-/*
+/**
  *  Mathods that perform precise DOM manipulation
  *  based on mutator method triggered
  */
@@ -130,7 +130,7 @@ module.exports = {
         this.retach()
     },
 
-    /*
+    /**
      *  Create a new child VM from a data object
      *  passing along compiler options indicating this
      *  is a sd-repeat item.
@@ -165,7 +165,7 @@ module.exports = {
         }
     },
 
-    /*
+    /**
      *  Update index of each item after a mutation
      */
     updateIndexes: function () {
@@ -175,7 +175,7 @@ module.exports = {
         }
     },
 
-    /*
+    /**
      *  Detach/ the container from the DOM before mutation
      *  so that batch DOM updates are done in-memory and faster
      */

+ 1 - 1
src/exp-parser.js

@@ -32,7 +32,7 @@ function getVariables (code) {
 
 module.exports = {
 
-    /*
+    /**
      *  Parse and create an anonymous computed property getter function
      *  from an arbitrary expression.
      */

+ 1 - 1
src/filters.js

@@ -29,7 +29,7 @@ module.exports = {
             : ''
     },
 
-    /*
+    /**
      *  args: an array of strings corresponding to
      *  the single, double, triple ... forms of the word to
      *  be pluralized. When the number to be pluralized

+ 10 - 10
src/main.js

@@ -4,7 +4,7 @@ var config      = require('./config'),
     filters     = require('./filters'),
     utils       = require('./utils')
 
-/*
+/**
  *  Set config options
  */
 ViewModel.config = function (opts) {
@@ -13,7 +13,7 @@ ViewModel.config = function (opts) {
     }
 }
 
-/*
+/**
  *  Allows user to register/retrieve a directive definition
  */
 ViewModel.directive = function (id, fn) {
@@ -21,7 +21,7 @@ ViewModel.directive = function (id, fn) {
     directives[id] = fn
 }
 
-/*
+/**
  *  Allows user to register/retrieve a filter function
  */
 ViewModel.filter = function (id, fn) {
@@ -29,7 +29,7 @@ ViewModel.filter = function (id, fn) {
     filters[id] = fn
 }
 
-/*
+/**
  *  Allows user to register/retrieve a ViewModel constructor
  */
 ViewModel.vm = function (id, Ctor) {
@@ -37,7 +37,7 @@ ViewModel.vm = function (id, Ctor) {
     utils.vms[id] = Ctor
 }
 
-/*
+/**
  *  Allows user to register/retrieve a template partial
  */
 ViewModel.partial = function (id, partial) {
@@ -45,7 +45,7 @@ ViewModel.partial = function (id, partial) {
     utils.partials[id] = templateToFragment(partial)
 }
 
-/*
+/**
  *  Allows user to register/retrieve a transition definition object
  */
 ViewModel.transition = function (id, transition) {
@@ -55,7 +55,7 @@ ViewModel.transition = function (id, transition) {
 
 ViewModel.extend = extend
 
-/*
+/**
  *  Expose the main ViewModel class
  *  and add extend method
  */
@@ -90,7 +90,7 @@ function extend (options) {
     return ExtendedVM
 }
 
-/*
+/**
  *  Inherit options
  *
  *  For options such as `scope`, `vms`, `directives`, 'partials',
@@ -118,7 +118,7 @@ function inheritOptions (child, parent, topLevel) {
     return child
 }
 
-/*
+/**
  *  Convert an object of partials to dom fragments
  */
 function convertPartials (partials) {
@@ -130,7 +130,7 @@ function convertPartials (partials) {
     }
 }
 
-/*
+/**
  *  Convert a string template to a dom fragment
  */
 function templateToFragment (template) {

+ 8 - 8
src/observer.js

@@ -48,7 +48,7 @@ for (var method in extensions) {
     def(ArrayProxy, method, extensions[method], !hasProto)
 }
 
-/*
+/**
  *  Watch an object based on type
  */
 function watch (obj, path, observer) {
@@ -60,7 +60,7 @@ function watch (obj, path, observer) {
     }
 }
 
-/*
+/**
  *  Watch an Object, recursive.
  */
 function watchObject (obj, path, observer) {
@@ -72,7 +72,7 @@ function watchObject (obj, path, observer) {
     }
 }
 
-/*
+/**
  *  Watch an Array, overload mutation methods
  *  and add augmentations by intercepting the prototype chain
  */
@@ -88,7 +88,7 @@ function watchArray (arr, path, observer) {
     }
 }
 
-/*
+/**
  *  Define accessors for a property on an Object
  *  so it emits get/set events.
  *  Then watch the value itself.
@@ -119,7 +119,7 @@ function bind (obj, key, path, observer) {
     watch(val, fullKey, observer)
 }
 
-/*
+/**
  *  Check if a value is watchable
  */
 function isWatchable (obj) {
@@ -127,7 +127,7 @@ function isWatchable (obj) {
     return type === 'Object' || type === 'Array'
 }
 
-/*
+/**
  *  When a value that is already converted is
  *  observed again by another observer, we can skip
  *  the watch conversion and simply emit set event for
@@ -150,7 +150,7 @@ module.exports = {
     // used in sd-repeat
     watchArray: watchArray,
 
-    /*
+    /**
      *  Observe an object with a given path,
      *  and proxy get/set/mutate events to the provided observer.
      */
@@ -194,7 +194,7 @@ module.exports = {
         }
     },
 
-    /*
+    /**
      *  Cancel observation, turn off the listeners.
      */
     unobserve: function (obj, path, observer) {

+ 1 - 1
src/text-parser.js

@@ -2,7 +2,7 @@ var BINDING_RE = /\{\{(.+?)\}\}/
 
 module.exports = {
 
-    /*
+    /**
      *  Parse a piece of text, return an array of tokens
      */
     parse: function (text) {

+ 6 - 6
src/utils.js

@@ -11,7 +11,7 @@ module.exports = {
     partials    : {},
     transitions : {},
 
-    /*
+    /**
      *  Define an ienumerable property
      *  This avoids it being included in JSON.stringify
      *  or for...in loops.
@@ -25,14 +25,14 @@ module.exports = {
         })
     },
 
-    /*
+    /**
      *  Accurate type check
      */
     typeOf: function (obj) {
         return toString.call(obj).slice(8, -1)
     },
 
-    /*
+    /**
      *  Make sure only strings and numbers are output to html
      *  output empty string is value is not string or number
      */
@@ -44,7 +44,7 @@ module.exports = {
             : ''
     },
 
-    /*
+    /**
      *  simple extend
      */
     extend: function (obj, ext, protective) {
@@ -55,7 +55,7 @@ module.exports = {
         }
     },
 
-    /*
+    /**
      *  log for debugging
      */
     log: function () {
@@ -64,7 +64,7 @@ module.exports = {
         }
     },
     
-    /*
+    /**
      *  warn for debugging
      */
     warn: function() {

+ 30 - 28
src/viewmodel.js

@@ -1,6 +1,7 @@
-var Compiler = require('./compiler')
+var Compiler = require('./compiler'),
+    def      = require('./utils').defProtected
 
-/*
+/**
  *  ViewModel exposed to the user that holds data,
  *  computed properties, event handlers
  *  and a few reserved methods
@@ -10,13 +11,15 @@ function ViewModel (options) {
     new Compiler(this, options)
 }
 
+// All VM prototype methods are inenumerable
+// so it can be stringified/looped through as raw data
 var VMProto = ViewModel.prototype
 
-/*
+/**
  *  Convenience function to set an actual nested value
  *  from a flat key string. Used in directives.
  */
-VMProto.$set = function (key, value) {
+def(VMProto, '$set', function (key, value) {
     var path = key.split('.'),
         obj = getTargetVM(this, path)
     if (!obj) return
@@ -24,14 +27,14 @@ VMProto.$set = function (key, value) {
         obj = obj[path[d]]
     }
     obj[path[d]] = value
-}
+})
 
-/*
+/**
  *  The function for getting a key
  *  which will go up along the prototype chain of the bindings
  *  Used in exp-parser.
  */
-VMProto.$get = function (key) {
+def(VMProto, '$get', function (key) {
     var path = key.split('.'),
         obj = getTargetVM(this, path),
         vm = obj
@@ -41,20 +44,20 @@ VMProto.$get = function (key) {
     }
     if (typeof obj === 'function') obj = obj.bind(vm)
     return obj
-}
+})
 
-/*
+/**
  *  watch a key on the viewmodel for changes
  *  fire callback with new value
  */
-VMProto.$watch = function (key, callback) {
+def(VMProto, '$watch', function (key, callback) {
     this.$compiler.observer.on('change:' + key, callback)
-}
+})
 
-/*
+/**
  *  unwatch a key
  */
-VMProto.$unwatch = function (key, callback) {
+def(VMProto, '$unwatch', function (key, callback) {
     // workaround here
     // since the emitter module checks callback existence
     // by checking the length of arguments
@@ -62,20 +65,19 @@ VMProto.$unwatch = function (key, callback) {
         ob = this.$compiler.observer
     if (callback) args.push(callback)
     ob.off.apply(ob, args)
-}
+})
 
-/*
+/**
  *  unbind everything, remove everything
  */
-VMProto.$destroy = function () {
+def(VMProto, '$destroy', function () {
     this.$compiler.destroy()
-    this.$compiler = null
-}
+})
 
-/*
+/**
  *  broadcast an event to all child VMs recursively.
  */
-VMProto.$broadcast = function () {
+def(VMProto, '$broadcast', function () {
     var children = this.$compiler.childCompilers,
         i = children.length,
         child
@@ -84,30 +86,30 @@ VMProto.$broadcast = function () {
         child.emitter.emit.apply(child.emitter, arguments)
         child.vm.$broadcast.apply(child.vm, arguments)
     }
-}
+})
 
-/*
+/**
  *  emit an event that propagates all the way up to parent VMs.
  */
-VMProto.$emit = function () {
+def(VMProto, '$emit', function () {
     var parent = this.$compiler.parentCompiler
     if (parent) {
         parent.emitter.emit.apply(parent.emitter, arguments)
         parent.vm.$emit.apply(parent.vm, arguments)
     }
-}
+})
 
-/*
+/**
  *  delegate on/off/once to the compiler's emitter
  */
 ;['on', 'off', 'once'].forEach(function (method) {
-    VMProto['$' + method] = function () {
+    def(VMProto, '$' + method, function () {
         var emitter = this.$compiler.emitter
         emitter[method].apply(emitter, arguments)
-    }
+    })
 })
 
-/*
+/**
  *  If a VM doesn't contain a path, go up the prototype chain
  *  to locate the ancestor that has it.
  */

+ 0 - 8
test/unit/specs/binding.js

@@ -135,14 +135,6 @@ describe('UNIT: Binding', function () {
             assert.ok(notInSubs2)
         })
 
-        it('should unref all instance props', function () {
-            assert.strictEqual(b.compiler, null)
-            assert.strictEqual(b.pubs, null)
-            assert.strictEqual(b.subs, null)
-            assert.strictEqual(b.instances, null)
-            assert.strictEqual(b.deps, null)
-        })
-
     })
 
 })