Przeglądaj źródła

add utils.extend

Evan You 13 lat temu
rodzic
commit
e1ce623035
6 zmienionych plików z 26 dodań i 52 usunięć
  1. 0 2
      TODO.md
  2. 3 9
      src/compiler.js
  3. 1 1
      src/deps-parser.js
  4. 9 20
      src/directive-parser.js
  5. 4 12
      src/main.js
  6. 9 8
      src/utils.js

+ 0 - 2
TODO.md

@@ -1,7 +1,5 @@
-- fix todomvc example.
 - consult https://github.com/RubyLouvre/avalon/issues/11 to allow simple expressions in directives.
 - consult https://github.com/RubyLouvre/avalon/issues/11 to allow simple expressions in directives.
 - $watch / $unwatch (now much easier)
 - $watch / $unwatch (now much easier)
-- add a few util methods, e.g. extend, inherits, traverse
 - tests
 - tests
 - docs
 - docs
 - sd-with?
 - sd-with?

+ 3 - 9
src/compiler.js

@@ -22,17 +22,11 @@ function Compiler (vm, options) {
 
 
     // copy options
     // copy options
     options = options || {}
     options = options || {}
-    for (var key in options) {
-        this[key] = options[key]
-    }
+    utils.extend(this, options)
 
 
     // copy data if any
     // copy data if any
     var data = options.data
     var data = options.data
-    if (data) {
-        for (key in data) {
-            vm[key] = data[key]
-        }
-    }
+    if (data) utils.extend(vm, data)
 
 
     // determine el
     // determine el
     var tpl = options.template,
     var tpl = options.template,
@@ -96,7 +90,7 @@ function Compiler (vm, options) {
     this.compileNode(this.el, true)
     this.compileNode(this.el, true)
 
 
     // for anything in viewmodel but not binded in DOM, also create bindings for them
     // for anything in viewmodel but not binded in DOM, also create bindings for them
-    for (key in vm) {
+    for (var key in vm) {
         if (vm.hasOwnProperty(key) &&
         if (vm.hasOwnProperty(key) &&
             key.charAt(0) !== '$' &&
             key.charAt(0) !== '$' &&
             !this.bindings.hasOwnProperty(key))
             !this.bindings.hasOwnProperty(key))

+ 1 - 1
src/deps-parser.js

@@ -5,7 +5,7 @@ var Emitter  = require('emitter'),
 
 
 var dummyEl = document.createElement('div'),
 var dummyEl = document.createElement('div'),
     ARGS_RE = /^function\s*?\((.+?)[\),]/,
     ARGS_RE = /^function\s*?\((.+?)[\),]/,
-    SCOPE_RE_STR = '\\.vm\\.[\\.A-Za-z0-9_][\\.A-Za-z0-9_$]*',
+    SCOPE_RE_STR = '\\.vm\\.[\\.\\w][\\.\\w$]*',
     noop = function () {}
     noop = function () {}
 
 
 /*
 /*

+ 9 - 20
src/directive-parser.js

@@ -8,35 +8,29 @@ var KEY_RE          = /^[^\|<]+/,
     FILTERS_RE      = /\|[^\|<]+/g,
     FILTERS_RE      = /\|[^\|<]+/g,
     FILTER_TOKEN_RE = /[^\s']+|'[^']+'/g,
     FILTER_TOKEN_RE = /[^\s']+|'[^']+'/g,
     INVERSE_RE      = /^!/,
     INVERSE_RE      = /^!/,
-    NESTING_RE      = /^\^+/,
-    ONEWAY_RE       = /-oneway$/
+    NESTING_RE      = /^\^+/
 
 
 /*
 /*
  *  Directive class
  *  Directive class
  *  represents a single directive instance in the DOM
  *  represents a single directive instance in the DOM
  */
  */
-function Directive (directiveName, expression, oneway) {
+function Directive (directiveName, expression) {
 
 
-    var prop,
-        definition = directives[directiveName]
+    var definition = directives[directiveName]
 
 
     // mix in properties from the directive definition
     // mix in properties from the directive definition
     if (typeof definition === 'function') {
     if (typeof definition === 'function') {
         this._update = definition
         this._update = definition
     } else {
     } else {
-        this._update = definition.update
-        for (prop in definition) {
-            if (prop !== 'update') {
-                if (prop === 'unbind') {
-                    this._unbind = definition[prop]
-                } else {
-                    this[prop] = definition[prop]
-                }
+        for (var prop in definition) {
+            if (prop === 'unbind' || prop === 'update') {
+                this['_' + prop] = definition[prop]
+            } else {
+                this[prop] = definition[prop]
             }
             }
         }
         }
     }
     }
 
 
-    this.oneway        = !!oneway
     this.directiveName = directiveName
     this.directiveName = directiveName
     this.expression    = expression.trim()
     this.expression    = expression.trim()
     this.rawKey        = expression.match(KEY_RE)[0].trim()
     this.rawKey        = expression.match(KEY_RE)[0].trim()
@@ -182,11 +176,6 @@ module.exports = {
         if (dirname.indexOf(prefix) === -1) return null
         if (dirname.indexOf(prefix) === -1) return null
         dirname = dirname.slice(prefix.length + 1)
         dirname = dirname.slice(prefix.length + 1)
 
 
-        var oneway = ONEWAY_RE.test(dirname)
-        if (oneway) {
-            dirname = dirname.slice(0, -7)
-        }
-
         var dir   = directives[dirname],
         var dir   = directives[dirname],
             valid = KEY_RE.test(expression)
             valid = KEY_RE.test(expression)
 
 
@@ -194,7 +183,7 @@ module.exports = {
         if (!valid) utils.warn('invalid directive expression: ' + expression)
         if (!valid) utils.warn('invalid directive expression: ' + expression)
 
 
         return dir && valid
         return dir && valid
-            ? new Directive(dirname, expression, oneway)
+            ? new Directive(dirname, expression)
             : null
             : null
     }
     }
 }
 }

+ 4 - 12
src/main.js

@@ -40,11 +40,7 @@ api.filter = function (name, fn) {
  *  Set config options
  *  Set config options
  */
  */
 api.config = function (opts) {
 api.config = function (opts) {
-    if (opts) {
-        for (var key in opts) {
-            config[key] = opts[key]
-        }
-    }
+    if (opts) utils.extend(config, opts)
     textParser.buildRegex()
     textParser.buildRegex()
 }
 }
 
 
@@ -79,13 +75,9 @@ ViewModel.extend = function (options) {
         }
         }
         ViewModel.call(this, opts)
         ViewModel.call(this, opts)
     }
     }
-    var p = ExtendedVM.prototype = Object.create(ViewModel.prototype)
-    p.constructor = ExtendedVM
-    if (options.props) {
-        for (var prop in options.props) {
-            p[prop] = options.props[prop]
-        }
-    }
+    var proto = ExtendedVM.prototype = Object.create(ViewModel.prototype)
+    proto.constructor = ExtendedVM
+    if (options.props) utils.extend(proto, options.props)
     if (options.id) {
     if (options.id) {
         utils.registerVM(options.id, ExtendedVM)
         utils.registerVM(options.id, ExtendedVM)
     }
     }

+ 9 - 8
src/utils.js

@@ -3,16 +3,17 @@ var config        = require('./config'),
     templates     = {},
     templates     = {},
     VMs           = {}
     VMs           = {}
 
 
-/*
- *  get accurate type of an object
- */
-function typeOf (obj) {
-    return toString.call(obj).slice(8, -1)
-}
-
 module.exports = {
 module.exports = {
 
 
-    typeOf: typeOf,
+    typeOf: function (obj) {
+        return toString.call(obj).slice(8, -1)
+    },
+
+    extend: function (obj, ext) {
+        for (var key in ext) {
+            obj[key] = ext[key]
+        }
+    },
 
 
     collectTemplates: function () {
     collectTemplates: function () {
         var selector = 'script[type="text/' + config.prefix + '-template"]',
         var selector = 'script[type="text/' + config.prefix + '-template"]',