mirror of https://github.com/vuejs/vue.git
				
				
				
			
		
			
				
	
	
		
			174 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			174 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
| var Watcher = require('../watcher')
 | |
| var Path = require('../parsers/path')
 | |
| var textParser = require('../parsers/text')
 | |
| var dirParser = require('../parsers/directive')
 | |
| var expParser = require('../parsers/expression')
 | |
| var filterRE = /[^|]\|[^|]/
 | |
| 
 | |
| /**
 | |
|  * Get the value from an expression on this vm.
 | |
|  *
 | |
|  * @param {String} exp
 | |
|  * @param {Boolean} [asStatement]
 | |
|  * @return {*}
 | |
|  */
 | |
| 
 | |
| exports.$get = function (exp, asStatement) {
 | |
|   var res = expParser.parse(exp)
 | |
|   if (res) {
 | |
|     if (asStatement && !expParser.isSimplePath(exp)) {
 | |
|       var self = this
 | |
|       return function statementHandler () {
 | |
|         res.get.call(self, self)
 | |
|       }
 | |
|     } else {
 | |
|       try {
 | |
|         return res.get.call(this, this)
 | |
|       } catch (e) {}
 | |
|     }
 | |
|   }
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Set the value from an expression on this vm.
 | |
|  * The expression must be a valid left-hand
 | |
|  * expression in an assignment.
 | |
|  *
 | |
|  * @param {String} exp
 | |
|  * @param {*} val
 | |
|  */
 | |
| 
 | |
| exports.$set = function (exp, val) {
 | |
|   var res = expParser.parse(exp, true)
 | |
|   if (res && res.set) {
 | |
|     res.set.call(this, this, val)
 | |
|   }
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Delete a property on the VM
 | |
|  *
 | |
|  * @param {String} key
 | |
|  */
 | |
| 
 | |
| exports.$delete = function (key) {
 | |
|   this._data.$delete(key)
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Watch an expression, trigger callback when its
 | |
|  * value changes.
 | |
|  *
 | |
|  * @param {String|Function} expOrFn
 | |
|  * @param {Function} cb
 | |
|  * @param {Object} [options]
 | |
|  *                 - {Boolean} deep
 | |
|  *                 - {Boolean} immediate
 | |
|  * @return {Function} - unwatchFn
 | |
|  */
 | |
| 
 | |
| exports.$watch = function (expOrFn, cb, options) {
 | |
|   var vm = this
 | |
|   var parsed
 | |
|   if (typeof expOrFn === 'string') {
 | |
|     parsed = dirParser.parse(expOrFn)
 | |
|     expOrFn = parsed.expression
 | |
|   }
 | |
|   var watcher = new Watcher(vm, expOrFn, cb, {
 | |
|     deep: options && options.deep,
 | |
|     filters: parsed && parsed.filters
 | |
|   })
 | |
|   if (options && options.immediate) {
 | |
|     cb.call(vm, watcher.value)
 | |
|   }
 | |
|   return function unwatchFn () {
 | |
|     watcher.teardown()
 | |
|   }
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Evaluate a text directive, including filters.
 | |
|  *
 | |
|  * @param {String} text
 | |
|  * @param {Boolean} [asStatement]
 | |
|  * @return {String}
 | |
|  */
 | |
| 
 | |
| exports.$eval = function (text, asStatement) {
 | |
|   // check for filters.
 | |
|   if (filterRE.test(text)) {
 | |
|     var dir = dirParser.parse(text)
 | |
|     // the filter regex check might give false positive
 | |
|     // for pipes inside strings, so it's possible that
 | |
|     // we don't get any filters here
 | |
|     var val = this.$get(dir.expression, asStatement)
 | |
|     return dir.filters
 | |
|       ? this._applyFilters(val, null, dir.filters)
 | |
|       : val
 | |
|   } else {
 | |
|     // no filter
 | |
|     return this.$get(text, asStatement)
 | |
|   }
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Interpolate a piece of template text.
 | |
|  *
 | |
|  * @param {String} text
 | |
|  * @return {String}
 | |
|  */
 | |
| 
 | |
| exports.$interpolate = function (text) {
 | |
|   var tokens = textParser.parse(text)
 | |
|   var vm = this
 | |
|   if (tokens) {
 | |
|     if (tokens.length === 1) {
 | |
|       return vm.$eval(tokens[0].value) + ''
 | |
|     } else {
 | |
|       return tokens.map(function (token) {
 | |
|         return token.tag
 | |
|           ? vm.$eval(token.value)
 | |
|           : token.value
 | |
|       }).join('')
 | |
|     }
 | |
|   } else {
 | |
|     return text
 | |
|   }
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Log instance data as a plain JS object
 | |
|  * so that it is easier to inspect in console.
 | |
|  * This method assumes console is available.
 | |
|  *
 | |
|  * @param {String} [path]
 | |
|  */
 | |
| 
 | |
| exports.$log = function (path) {
 | |
|   var data = path
 | |
|     ? Path.get(this._data, path)
 | |
|     : this._data
 | |
|   if (data) {
 | |
|     data = clean(data)
 | |
|   }
 | |
|   // include computed fields
 | |
|   if (!path) {
 | |
|     for (var key in this.$options.computed) {
 | |
|       data[key] = clean(this[key])
 | |
|     }
 | |
|   }
 | |
|   console.log(data)
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * "clean" a getter/setter converted object into a plain
 | |
|  * object copy.
 | |
|  *
 | |
|  * @param {Object} - obj
 | |
|  * @return {Object}
 | |
|  */
 | |
| 
 | |
| function clean (obj) {
 | |
|   return JSON.parse(JSON.stringify(obj))
 | |
| }
 |