Browse Source

working on global api

Evan You 11 years ago
parent
commit
aa24b3e4ad
8 changed files with 182 additions and 45 deletions
  1. 1 0
      explorations/inheritance.js
  2. 69 34
      src/api/global.js
  3. 46 7
      src/config.js
  4. 0 0
      src/directives/index.js
  5. 0 0
      src/filters/index.js
  6. 1 2
      src/instance/scope.js
  7. 59 0
      src/util.js
  8. 6 2
      src/vue.js

+ 1 - 0
explorations/inheritance.js

@@ -30,6 +30,7 @@ window.child = new Vue({
 
 window.item = new Vue({
   parent: vm,
+  syncData: true,
   data: vm.arr[0]
 })
 

+ 69 - 34
src/api/global.js

@@ -2,60 +2,95 @@ var _ = require('../util')
 var config = require('../config')
 
 /**
- * Configuration
+ * Vue and every constructor that extends Vue has an associated
+ * options object, which can be accessed during compilation steps
+ * as `this.constructor.options`.
  */
 
-exports.config = function () {
-  
+exports.options = {
+  directives : require('../directives'),
+  filters    : require('../filters'),
+  partials   : {},
+  effects    : {},
+  components : {}
 }
 
 /**
- * Class inehritance
+ * Expose useful internals
  */
 
-exports.extend = function () {
-  
-}
+exports.util = _
+exports.config = config
+exports.nextTick = _.nextTick
+exports.transition = require('../transition/transition')
 
 /**
- * Plugin system
+ * Class inehritance
+ *
+ * @param {Object} extendOptions
  */
 
-exports.use = function () {
-  
+exports.extend = function (extendOptions) {
+  var Super = this
+  var Sub = function (instanceOptions) {
+    var mergedOptions = _.mergeOptions(Sub.options, instanceOptions)
+    Super.call(this, mergedOptions)
+  }
+  Sub.prototype = Object.create(Super.prototype)
+  _.define(Sub.prototype, 'constructor', Sub)
+  Sub.options = _.mergeOptions(Super.options, extendOptions)
+  Sub.super = Super
+  // allow further extension
+  Sub.extend = Super.extend
+  // create asset registers, so extended classes
+  // can have their private assets too.
+  createAssetRegisters(Sub)
 }
 
 /**
- * Expose some internal utilities
+ * Plugin system
+ *
+ * @param {String|Object} plugin
  */
 
-exports.require = function () {
-  
+exports.use = function (plugin) {
+  if (typeof plugin === 'string') {
+    try {
+      plugin = require(plugin)
+    } catch (e) {
+      _.warn('Cannot load plugin: ' + plugin)
+    }
+  }
+  // additional parameters
+  var args = [].slice.call(arguments, 1)
+  args.unshift(this)
+  if (typeof plugin.install === 'function') {
+    plugin.install.apply(plugin, args)
+  } else {
+    plugin.apply(null, args)
+  }
+  return this
 }
 
 /**
- * Define asset registries and registration
- * methods on a constructor.
+ * Define asset registration methods on a constructor.
+ *
+ * @param {Function} Ctor
  */
 
-config.assetTypes.forEach(function (type) {
-  var registry = '_' + type + 's'
-  exports[registry] = {}
-
-  /**
-   * Asset registration method.
-   *
-   * @param {String} id
-   * @param {*} definition
-   */
-
-  exports[type] = function (id, definition) {
-    this[registry][id] = definition
-  }
-})
+createAssetRegisters(exports)
+function createAssetRegisters (Ctor) {
+  config.assetTypes.forEach(function (type) {
 
-/**
- * This is pretty useful so we expose it as a global method.
- */
+    /**
+     * Asset registration method.
+     *
+     * @param {String} id
+     * @param {*} definition
+     */
 
-exports.nextTick = _.nextTick
+    Ctor[type] = function (id, definition) {
+      this.options[type + 's'][id] = definition
+    }
+  })
+}

+ 46 - 7
src/config.js

@@ -1,11 +1,50 @@
+var assetTypes = [
+  'directive',
+  'filter',
+  'partial',
+  'effect',
+  'component'
+]
+
 module.exports = {
 
-  assetTypes: [
-    'directive',
-    'filter',
-    'partial',
-    'effect',
-    'component'
-  ]
+  /**
+   * The prefix to look for when parsing directives.
+   * @type {String}
+   */
+
+  prefix: 'v',
+
+  /**
+   * Whether to print debug messages.
+   * Also enables stack trace for warnings.
+   * @type {Boolean}
+   */
+
+  debug: false,
+
+  /**
+   * Whether to suppress warnings.
+   * @type {Boolean}
+   */
+
+  silent: false,
+
+  /**
+   * Whether to parse mustache tags in templates.
+   * @type {Boolean}
+   */
+
+  interpolate: true,
+
+  /**
+   * Asset types
+   * @type {Array.<String>}
+   * @readonly
+   */
+
+  get assetTypes () {
+    return assetTypes
+  }
 
 }

+ 0 - 0
src/directives/index.js


+ 0 - 0
src/filters/index.js


+ 1 - 2
src/instance/scope.js

@@ -11,8 +11,7 @@ var scopeEvents = ['set', 'mutate', 'add', 'delete']
  */
 
 exports._initScope = function () {
-  var options = this.$options
-  var parent = this.$parent = options.parent
+  var parent = this.$parent = this.$options.parent
   var scope = this.$scope = parent
     ? Object.create(parent.$scope)
     : {}

+ 59 - 0
src/util.js

@@ -1,3 +1,5 @@
+var config = require('./config')
+
 /**
  * Mix properties into target object.
  *
@@ -105,4 +107,61 @@ if ('__proto__' in {}) {
   }
 } else {
   exports.augment = exports.deepMixin
+}
+
+/**
+ * Merge two option objects.
+ *
+ * @param {Object} parent
+ * @param {Object} child
+ * @param {Boolean} noRecurse
+ */
+
+exports.mergeOptions = function (parent, child, noRecurse) {
+  // TODO
+  // - merge lifecycle hooks
+  // - merge asset registries
+  // - else override
+  // - use prototypal inheritance where appropriate
+}
+
+/**
+ * Enable debug utilities. The enableDebug() function and all
+ * _.log() & _.warn() calls will be dropped in the minified
+ * production build.
+ */
+
+enableDebug()
+
+function enableDebug () {
+
+  var hasConsole = typeof console !== 'undefined'
+  
+  /**
+   * Log a message.
+   *
+   * @param {String} msg
+   */
+
+  exports.log = function (msg) {
+    if (hasConsole && config.debug) {
+      console.log(msg)
+    }
+  }
+
+  /**
+   * We've got a problem here.
+   *
+   * @param {String} msg
+   */
+
+  exports.warn = function (msg) {
+    if (hasConsole && !config.silent) {
+      console.warn(msg)
+      if (config.debug && console.trace) {
+        console.trace(msg)
+      }
+    }
+  }
+
 }

+ 6 - 2
src/vue.js

@@ -14,17 +14,21 @@ var _ = require('./util')
  */
 
 function Vue (options) {
-  this.$options = options = options || {}
+  this.$options = options || {}
   // create scope
   this._initScope()
   // setup initial data.
-  this._initData(options.data || {}, true)
+  this._initData(this.$options.data || {}, true)
   // setup property proxying
   this._initProxy()
   // setup binding tree
   this._initBindings()
 }
 
+/**
+ * Build up the prototype
+ */
+
 var p = Vue.prototype
 
 /**