data.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. var _ = require('../util')
  2. var Watcher = require('../watcher')
  3. var textParser = require('../parse/text')
  4. var dirParser = require('../parse/directive')
  5. var expParser = require('../parse/expression')
  6. var filterRE = /[^|]\|[^|]/
  7. /**
  8. * Get the value from an expression on this vm.
  9. *
  10. * @param {String} exp
  11. * @return {*}
  12. */
  13. exports.$get = function (exp) {
  14. var res = expParser.parse(exp)
  15. if (res) {
  16. return res.get.call(this, this)
  17. }
  18. }
  19. /**
  20. * Set the value from an expression on this vm.
  21. * The expression must be a valid left-hand
  22. * expression in an assignment.
  23. *
  24. * @param {String} exp
  25. * @param {*} val
  26. */
  27. exports.$set = function (exp, val) {
  28. var res = expParser.parse(exp, true)
  29. if (res && res.set) {
  30. res.set.call(this, this, val)
  31. }
  32. }
  33. /**
  34. * Add a property on the VM
  35. *
  36. * @param {String} key
  37. * @param {*} val
  38. */
  39. exports.$add = function (key, val) {
  40. this._data.$add(key, val)
  41. }
  42. /**
  43. * Delete a property on the VM
  44. *
  45. * @param {String} key
  46. */
  47. exports.$delete = function (key) {
  48. this._data.$delete(key)
  49. }
  50. /**
  51. * Watch an expression, trigger callback when its
  52. * value changes.
  53. *
  54. * @param {String} exp
  55. * @param {Function} cb
  56. * @param {Boolean} [immediate]
  57. * @return {Function} - unwatchFn
  58. */
  59. exports.$watch = function (exp, cb, immediate) {
  60. var vm = this
  61. var watcher = vm._userWatchers[exp]
  62. var wrappedCb = function (val, oldVal) {
  63. cb.call(vm, val, oldVal)
  64. }
  65. if (!watcher) {
  66. watcher = vm._userWatchers[exp] =
  67. new Watcher(vm, exp, wrappedCb)
  68. } else {
  69. watcher.addCb(wrappedCb)
  70. }
  71. if (immediate) {
  72. wrappedCb(watcher.value)
  73. }
  74. return function unwatchFn () {
  75. watcher.removeCb(wrappedCb)
  76. if (!watcher.active) {
  77. vm._userWatchers[exp] = null
  78. }
  79. }
  80. }
  81. /**
  82. * Evaluate a text directive, including filters.
  83. *
  84. * @param {String} text
  85. * @return {String}
  86. */
  87. exports.$eval = function (text) {
  88. // check for filters.
  89. if (filterRE.test(text)) {
  90. var dir = dirParser.parse(text)[0]
  91. // the filter regex check might give false positive
  92. // for pipes inside strings, so it's possible that
  93. // we don't get any filters here
  94. return dir.filters
  95. ? _.applyFilters(
  96. this.$get(dir.expression),
  97. _.resolveFilters(this, dir.filters).read,
  98. this
  99. )
  100. : this.$get(dir.expression)
  101. } else {
  102. // no filter
  103. return this.$get(text)
  104. }
  105. }
  106. /**
  107. * Interpolate a piece of template text.
  108. *
  109. * @param {String} text
  110. * @return {String}
  111. */
  112. exports.$interpolate = function (text) {
  113. var tokens = textParser.parse(text)
  114. var vm = this
  115. if (tokens) {
  116. return tokens.length === 1
  117. ? vm.$eval(tokens[0].value)
  118. : tokens.map(function (token) {
  119. return token.tag
  120. ? vm.$eval(token.value)
  121. : token.value
  122. }).join('')
  123. } else {
  124. return text
  125. }
  126. }
  127. /**
  128. * Log instance data as a plain JS object
  129. * so that it is easier to inspect in console.
  130. * This method assumes console is available.
  131. *
  132. * @param {String} [key]
  133. */
  134. exports.$log = function (key) {
  135. var data = this[key || '_data']
  136. console.log(JSON.parse(JSON.stringify(data)))
  137. }