github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/src/instance/api/data.js (about) 1 import Watcher from '../../watcher' 2 import { del, toArray } from '../../util/index' 3 import { parseText } from '../../parsers/text' 4 import { parseDirective } from '../../parsers/directive' 5 import { getPath } from '../../parsers/path' 6 import { parseExpression } from '../../parsers/expression' 7 8 const filterRE = /[^|]\|[^|]/ 9 10 export default function (Vue) { 11 /** 12 * Get the value from an expression on this vm. 13 * 14 * @param {String} exp 15 * @param {Boolean} [asStatement] 16 * @return {*} 17 */ 18 19 Vue.prototype.$get = function (exp, asStatement) { 20 var res = parseExpression(exp) 21 if (res) { 22 if (asStatement) { 23 var self = this 24 return function statementHandler () { 25 self.$arguments = toArray(arguments) 26 var result = res.get.call(self, self) 27 self.$arguments = null 28 return result 29 } 30 } else { 31 try { 32 return res.get.call(this, this) 33 } catch (e) {} 34 } 35 } 36 } 37 38 /** 39 * Set the value from an expression on this vm. 40 * The expression must be a valid left-hand 41 * expression in an assignment. 42 * 43 * @param {String} exp 44 * @param {*} val 45 */ 46 47 Vue.prototype.$set = function (exp, val) { 48 var res = parseExpression(exp, true) 49 if (res && res.set) { 50 res.set.call(this, this, val) 51 } 52 } 53 54 /** 55 * Delete a property on the VM 56 * 57 * @param {String} key 58 */ 59 60 Vue.prototype.$delete = function (key) { 61 del(this._data, key) 62 } 63 64 /** 65 * Watch an expression, trigger callback when its 66 * value changes. 67 * 68 * @param {String|Function} expOrFn 69 * @param {Function} cb 70 * @param {Object} [options] 71 * - {Boolean} deep 72 * - {Boolean} immediate 73 * @return {Function} - unwatchFn 74 */ 75 76 Vue.prototype.$watch = function (expOrFn, cb, options) { 77 var vm = this 78 var parsed 79 if (typeof expOrFn === 'string') { 80 parsed = parseDirective(expOrFn) 81 expOrFn = parsed.expression 82 } 83 var watcher = new Watcher(vm, expOrFn, cb, { 84 deep: options && options.deep, 85 sync: options && options.sync, 86 filters: parsed && parsed.filters, 87 user: !options || options.user !== false 88 }) 89 if (options && options.immediate) { 90 cb.call(vm, watcher.value) 91 } 92 return function unwatchFn () { 93 watcher.teardown() 94 } 95 } 96 97 /** 98 * Evaluate a text directive, including filters. 99 * 100 * @param {String} text 101 * @param {Boolean} [asStatement] 102 * @return {String} 103 */ 104 105 Vue.prototype.$eval = function (text, asStatement) { 106 // check for filters. 107 if (filterRE.test(text)) { 108 var dir = parseDirective(text) 109 // the filter regex check might give false positive 110 // for pipes inside strings, so it's possible that 111 // we don't get any filters here 112 var val = this.$get(dir.expression, asStatement) 113 return dir.filters 114 ? this._applyFilters(val, null, dir.filters) 115 : val 116 } else { 117 // no filter 118 return this.$get(text, asStatement) 119 } 120 } 121 122 /** 123 * Interpolate a piece of template text. 124 * 125 * @param {String} text 126 * @return {String} 127 */ 128 129 Vue.prototype.$interpolate = function (text) { 130 var tokens = parseText(text) 131 var vm = this 132 if (tokens) { 133 if (tokens.length === 1) { 134 return vm.$eval(tokens[0].value) + '' 135 } else { 136 return tokens.map(function (token) { 137 return token.tag 138 ? vm.$eval(token.value) 139 : token.value 140 }).join('') 141 } 142 } else { 143 return text 144 } 145 } 146 147 /** 148 * Log instance data as a plain JS object 149 * so that it is easier to inspect in console. 150 * This method assumes console is available. 151 * 152 * @param {String} [path] 153 */ 154 155 Vue.prototype.$log = function (path) { 156 var data = path 157 ? getPath(this._data, path) 158 : this._data 159 if (data) { 160 data = clean(data) 161 } 162 // include computed fields 163 if (!path) { 164 var key 165 for (key in this.$options.computed) { 166 data[key] = clean(this[key]) 167 } 168 if (this._props) { 169 for (key in this._props) { 170 data[key] = clean(this[key]) 171 } 172 } 173 } 174 console.log(data) 175 } 176 177 /** 178 * "clean" a getter/setter converted object into a plain 179 * object copy. 180 * 181 * @param {Object} - obj 182 * @return {Object} 183 */ 184 185 function clean (obj) { 186 return JSON.parse(JSON.stringify(obj)) 187 } 188 }