| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- var config = require('./config'),
- ViewModel = require('./viewmodel'),
- utils = require('./utils'),
- makeHash = utils.hash,
- assetTypes = ['directive', 'filter', 'partial', 'transition', 'component']
- ViewModel.options = config.globalAssets = {
- directives : require('./directives'),
- filters : require('./filters'),
- partials : makeHash(),
- transitions : makeHash(),
- components : makeHash()
- }
- /**
- * Expose asset registration methods
- */
- assetTypes.forEach(function (type) {
- ViewModel[type] = function (id, value) {
- var hash = this.options[type + 's']
- if (!hash) {
- hash = this.options[type + 's'] = makeHash()
- }
- if (!value) return hash[id]
- if (type === 'partial') {
- value = utils.toFragment(value)
- } else if (type === 'component') {
- value = utils.toConstructor(value)
- }
- hash[id] = value
- return this
- }
- })
- /**
- * Set config options
- */
- ViewModel.config = function (opts, val) {
- if (typeof opts === 'string') {
- if (val === undefined) {
- return config[opts]
- } else {
- config[opts] = val
- }
- } else {
- utils.extend(config, opts)
- }
- return this
- }
- /**
- * Expose internal modules for plugins
- */
- ViewModel.require = function (path) {
- return require('./' + path)
- }
- /**
- * Expose an interface for plugins
- */
- ViewModel.use = function (plugin) {
- if (typeof plugin === 'string') {
- try {
- plugin = require(plugin)
- } catch (e) {
- return utils.warn('Cannot find plugin: ' + plugin)
- }
- }
- if (typeof plugin === 'function') {
- plugin(ViewModel)
- } else if (plugin.install) {
- plugin.install(ViewModel)
- }
- }
- ViewModel.extend = extend
- ViewModel.nextTick = utils.nextTick
- /**
- * Expose the main ViewModel class
- * and add extend method
- */
- function extend (options) {
- var ParentVM = this
- // inherit options
- options = inheritOptions(options, ParentVM.options, true)
- utils.processOptions(options)
- var ExtendedVM = function (opts, asParent) {
- if (!asParent) {
- opts = inheritOptions(opts, options, true)
- }
- ParentVM.call(this, opts, true)
- }
- // inherit prototype props
- var proto = ExtendedVM.prototype = Object.create(ParentVM.prototype)
- utils.defProtected(proto, 'constructor', ExtendedVM)
- // copy prototype props
- var methods = options.methods
- if (methods) {
- for (var key in methods) {
- if (
- !(key in ViewModel.prototype) &&
- typeof methods[key] === 'function'
- ) {
- proto[key] = methods[key]
- }
- }
- }
- // allow extended VM to be further extended
- ExtendedVM.extend = extend
- ExtendedVM.super = ParentVM
- ExtendedVM.options = options
- // allow extended VM to add its own assets
- assetTypes.forEach(function (type) {
- ExtendedVM[type] = ViewModel[type]
- })
- return ExtendedVM
- }
- /**
- * Inherit options
- *
- * For options such as `data`, `vms`, `directives`, 'partials',
- * they should be further extended. However extending should only
- * be done at top level.
- *
- * `proto` is an exception because it's handled directly on the
- * prototype.
- *
- * `el` is an exception because it's not allowed as an
- * extension option, but only as an instance option.
- */
- function inheritOptions (child, parent, topLevel) {
- child = child || makeHash()
- if (!parent) return child
- for (var key in parent) {
- if (key === 'el' || key === 'methods') continue
- var val = child[key],
- parentVal = parent[key],
- type = utils.typeOf(val)
- if (topLevel && type === 'Function' && parentVal) {
- // merge hook functions into an array
- child[key] = [val]
- if (Array.isArray(parentVal)) {
- child[key] = child[key].concat(parentVal)
- } else {
- child[key].push(parentVal)
- }
- } else if (topLevel && type === 'Object') {
- // merge toplevel object options
- inheritOptions(val, parentVal)
- } else if (val === undefined) {
- // inherit if child doesn't override
- child[key] = parentVal
- }
- }
- return child
- }
- module.exports = ViewModel
|