vue2/src/api/data.js

150 lines
2.9 KiB
JavaScript
Raw Normal View History

2014-08-11 01:32:54 +08:00
var Watcher = require('../watcher')
var Path = require('../parsers/path')
var textParser = require('../parsers/text')
var dirParser = require('../parsers/directive')
var expParser = require('../parsers/expression')
2014-08-11 10:17:30 +08:00
var filterRE = /[^|]\|[^|]/
2014-08-11 01:32:54 +08:00
/**
* Get the value from an expression on this vm.
*
* @param {String} exp
* @return {*}
*/
exports.$get = function (exp) {
var res = expParser.parse(exp)
if (res) {
2015-05-09 02:05:40 +08:00
return res.get.call(this, this)
2014-08-11 01:32:54 +08:00
}
2014-07-09 03:57:47 +08:00
}
2014-08-11 01:32:54 +08:00
/**
* Set the value from an expression on this vm.
2014-08-11 02:12:22 +08:00
* The expression must be a valid left-hand
* expression in an assignment.
2014-08-11 01:32:54 +08:00
*
* @param {String} exp
* @param {*} val
*/
exports.$set = function (exp, val) {
var res = expParser.parse(exp, true)
if (res && res.set) {
2014-08-25 11:47:36 +08:00
res.set.call(this, this, val)
2014-08-11 01:32:54 +08:00
}
2014-07-09 03:57:47 +08:00
}
2014-08-11 01:32:54 +08:00
/**
2014-08-11 02:12:22 +08:00
* Add a property on the VM
2014-08-11 01:32:54 +08:00
*
* @param {String} key
* @param {*} val
*/
exports.$add = function (key, val) {
2014-09-17 08:50:17 +08:00
this._data.$add(key, val)
2014-07-14 22:43:48 +08:00
}
2014-08-11 01:32:54 +08:00
/**
2014-08-11 02:12:22 +08:00
* Delete a property on the VM
2014-08-11 01:32:54 +08:00
*
* @param {String} key
*/
exports.$delete = function (key) {
2014-09-17 08:50:17 +08:00
this._data.$delete(key)
2014-07-14 22:43:48 +08:00
}
2014-08-11 01:32:54 +08:00
/**
2014-08-11 02:12:22 +08:00
* Watch an expression, trigger callback when its
2014-08-29 07:02:32 +08:00
* value changes.
2014-08-11 01:32:54 +08:00
*
* @param {String} exp
* @param {Function} cb
2014-09-18 23:29:01 +08:00
* @param {Boolean} [deep]
2014-08-18 02:13:52 +08:00
* @param {Boolean} [immediate]
2014-08-29 09:55:32 +08:00
* @return {Function} - unwatchFn
2014-08-11 01:32:54 +08:00
*/
2014-09-18 23:29:01 +08:00
exports.$watch = function (exp, cb, deep, immediate) {
2014-08-29 09:55:32 +08:00
var vm = this
var wrappedCb = function (val, oldVal) {
cb.call(vm, val, oldVal)
}
2015-05-27 08:55:27 +08:00
var watcher = new Watcher(vm, exp, wrappedCb, {
deep: deep,
user: true
})
2014-08-18 02:13:52 +08:00
if (immediate) {
2014-08-29 09:55:32 +08:00
wrappedCb(watcher.value)
2014-08-18 02:13:52 +08:00
}
2014-08-29 09:55:32 +08:00
return function unwatchFn () {
2015-05-27 08:55:27 +08:00
watcher.teardown()
2014-08-11 01:32:54 +08:00
}
2014-07-14 22:43:48 +08:00
}
2014-08-11 10:17:30 +08:00
/**
* Evaluate a text directive, including filters.
*
* @param {String} text
* @return {String}
*/
exports.$eval = function (text) {
// check for filters.
if (filterRE.test(text)) {
var dir = dirParser.parse(text)[0]
// the filter regex check might give false positive
// for pipes inside strings, so it's possible that
// we don't get any filters here
2015-05-29 03:13:35 +08:00
var val = this.$get(dir.expression)
2014-08-11 10:17:30 +08:00
return dir.filters
2015-05-29 03:13:35 +08:00
? this._applyFilters(val, null, dir.filters)
: val
2014-08-11 10:17:30 +08:00
} else {
// no filter
return this.$get(text)
}
}
2014-08-11 01:32:54 +08:00
/**
2014-08-11 04:46:20 +08:00
* Interpolate a piece of template text.
2014-08-11 01:32:54 +08:00
*
2014-08-11 04:46:20 +08:00
* @param {String} text
2014-08-11 02:12:22 +08:00
* @return {String}
2014-08-11 01:32:54 +08:00
*/
2014-08-11 04:46:20 +08:00
exports.$interpolate = function (text) {
2014-08-11 10:17:30 +08:00
var tokens = textParser.parse(text)
var vm = this
if (tokens) {
2014-08-16 17:41:18 +08:00
return tokens.length === 1
? vm.$eval(tokens[0].value)
: tokens.map(function (token) {
return token.tag
? vm.$eval(token.value)
: token.value
}).join('')
2014-08-11 10:17:30 +08:00
} else {
return text
}
2014-07-14 22:43:48 +08:00
}
2014-08-11 01:32:54 +08:00
/**
* Log instance data as a plain JS object
* so that it is easier to inspect in console.
* This method assumes console is available.
*
2014-11-06 12:29:50 +08:00
* @param {String} [path]
2014-08-11 01:32:54 +08:00
*/
2014-11-06 12:29:50 +08:00
exports.$log = function (path) {
var data = path
? Path.get(this._data, path)
2014-11-06 12:29:50 +08:00
: this._data
if (data) {
data = JSON.parse(JSON.stringify(data))
}
console.log(data)
2014-07-09 03:57:47 +08:00
}