scope.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. var _ = require('../util')
  2. var Observer = require('../observe/observer')
  3. var scopeEvents = ['set', 'mutate', 'add', 'delete']
  4. /**
  5. * Setup instance scope.
  6. * The scope is reponsible for prototypal inheritance of
  7. * parent instance propertiesm abd all binding paths and
  8. * expressions of the current instance are evaluated against
  9. * its scope.
  10. *
  11. * This should only be called once during _init().
  12. */
  13. exports._initScope = function () {
  14. var parent = this.$parent
  15. var inherit = parent && !this.$options.isolated
  16. var data = this._data
  17. var scope = this.$scope = inherit
  18. ? Object.create(parent.$scope)
  19. : {}
  20. // copy initial data into scope
  21. for (var key in data) {
  22. // use defineProperty so we can shadow parent accessors
  23. _.define(scope, key, data[key], true)
  24. }
  25. // create scope observer
  26. this.$observer = Observer.create(scope, {
  27. callbackContext: this,
  28. doNotAlterProto: true
  29. })
  30. // setup sync between data and the scope
  31. this._syncData()
  32. if (!inherit) {
  33. return
  34. }
  35. // relay change events that sent down from
  36. // the scope prototype chain.
  37. var ob = this.$observer
  38. var pob = parent.$observer
  39. var listeners = this._scopeListeners = {}
  40. scopeEvents.forEach(function (event) {
  41. var cb = listeners[event] = function (key, a, b) {
  42. // since these events come from upstream,
  43. // we only emit them if we don't have the same keys
  44. // shadowing them in current scope.
  45. if (!scope.hasOwnProperty(key)) {
  46. ob.emit(event, key, a, b)
  47. }
  48. }
  49. pob.on(event, cb)
  50. })
  51. }
  52. /**
  53. * Teardown scope, unsync data, and remove all listeners
  54. * including ones attached to parent's observer.
  55. * Only called once during $destroy().
  56. */
  57. exports._teardownScope = function () {
  58. this.$observer.off()
  59. this._unsyncData()
  60. this._data = null
  61. this.$scope = null
  62. if (this.$parent) {
  63. var pob = this.$parent.$observer
  64. var listeners = this._scopeListeners
  65. scopeEvents.forEach(function (event) {
  66. pob.off(event, listeners[event])
  67. })
  68. this._scopeListeners = null
  69. }
  70. }
  71. /**
  72. * Called when swapping the $data object.
  73. *
  74. * Old properties that are not present in new data are
  75. * deleted from the scope, and new data properties not
  76. * already on the scope are added. Teardown old data sync
  77. * listeners and setup new ones.
  78. *
  79. * @param {Object} data
  80. */
  81. exports._setData = function (data) {
  82. this._data = data
  83. var scope = this.$scope
  84. var key
  85. // teardown old sync listeners
  86. this._unsyncData()
  87. // delete keys not present in the new data
  88. for (key in scope) {
  89. if (
  90. key.charCodeAt(0) !== 0x24 && // $
  91. scope.hasOwnProperty(key) &&
  92. !(key in data)
  93. ) {
  94. scope.$delete(key)
  95. }
  96. }
  97. // copy properties into scope
  98. for (key in data) {
  99. if (scope.hasOwnProperty(key)) {
  100. // existing property, trigger set
  101. scope[key] = data[key]
  102. } else {
  103. // new property
  104. scope.$add(key, data[key])
  105. }
  106. }
  107. // setup sync between scope and new data
  108. if (!this.$options._noSync) {
  109. this._syncData()
  110. }
  111. }
  112. /**
  113. * Proxy the scope properties on the instance itself,
  114. * so that vm.a === vm.$scope.a.
  115. *
  116. * Note this only proxies *local* scope properties. We want
  117. * to prevent child instances accidentally modifying
  118. * properties with the same name up in the scope chain
  119. * because scope perperties are all getter/setters.
  120. *
  121. * To access parent properties through prototypal fall
  122. * through, access it on the instance's $scope.
  123. *
  124. * This should only be called once during _init().
  125. */
  126. exports._initProxy = function () {
  127. var scope = this.$scope
  128. // scope --> vm
  129. // proxy scope data on vm
  130. for (var key in scope) {
  131. if (scope.hasOwnProperty(key)) {
  132. _.proxy(this, scope, key)
  133. }
  134. }
  135. // keep proxying up-to-date with added/deleted keys.
  136. this.$observer
  137. .on('add:self', function (key) {
  138. _.proxy(this, scope, key)
  139. })
  140. .on('delete:self', function (key) {
  141. delete this[key]
  142. })
  143. // vm --> scope
  144. // proxy vm parent & root on scope
  145. _.proxy(scope, this, '$parent')
  146. _.proxy(scope, this, '$root')
  147. _.proxy(scope, this, '$data')
  148. }
  149. /**
  150. * Setup computed properties.
  151. * All computed properties are proxied onto the scope.
  152. * Because they are accessors their `this` context will
  153. * be the instance instead of the scope.
  154. */
  155. function noop () {}
  156. exports._initComputed = function () {
  157. var computed = this.$options.computed
  158. var scope = this.$scope
  159. if (computed) {
  160. for (var key in computed) {
  161. var def = computed[key]
  162. if (typeof def === 'function') {
  163. def = {
  164. get: def,
  165. set: noop
  166. }
  167. }
  168. def.enumerable = true
  169. def.configurable = true
  170. Object.defineProperty(this, key, def)
  171. _.proxy(scope, this, key)
  172. }
  173. }
  174. }
  175. /**
  176. * Setup instance methods.
  177. * Methods are also copied into scope, but they must
  178. * be bound to the instance.
  179. */
  180. exports._initMethods = function () {
  181. var methods = this.$options.methods
  182. var scope = this.$scope
  183. if (methods) {
  184. for (var key in methods) {
  185. var method = methods[key]
  186. this[key] = method
  187. scope[key] = _.bind(method, this)
  188. }
  189. }
  190. }
  191. /**
  192. * Setup two-way sync between the instance scope and
  193. * the original data. Requires teardown.
  194. */
  195. exports._syncData = function () {
  196. var data = this._data
  197. var scope = this.$scope
  198. var locked = false
  199. var listeners = this._syncListeners = {
  200. data: {
  201. set: guard(function (key, val) {
  202. data[key] = val
  203. }),
  204. add: guard(function (key, val) {
  205. data.$add(key, val)
  206. }),
  207. delete: guard(function (key) {
  208. data.$delete(key)
  209. })
  210. },
  211. scope: {
  212. set: guard(function (key, val) {
  213. scope[key] = val
  214. }),
  215. add: guard(function (key, val) {
  216. scope.$add(key, val)
  217. }),
  218. delete: guard(function (key) {
  219. scope.$delete(key)
  220. })
  221. }
  222. }
  223. // sync scope and original data.
  224. this.$observer
  225. .on('set:self', listeners.data.set)
  226. .on('add:self', listeners.data.add)
  227. .on('delete:self', listeners.data.delete)
  228. this._dataObserver = Observer.create(data)
  229. this._dataObserver
  230. .on('set:self', listeners.scope.set)
  231. .on('add:self', listeners.scope.add)
  232. .on('delete:self', listeners.scope.delete)
  233. /**
  234. * The guard function prevents infinite loop
  235. * when syncing between two observers. Also
  236. * filters out properties prefixed with $ or _.
  237. *
  238. * @param {Function} fn
  239. * @return {Function}
  240. */
  241. function guard (fn) {
  242. return function (key, val) {
  243. if (locked) {
  244. return
  245. }
  246. var c = key.charCodeAt(0)
  247. if (c === 0x24 || c === 0x5F) { // $ and _
  248. return
  249. }
  250. locked = true
  251. fn(key, val)
  252. locked = false
  253. }
  254. }
  255. }
  256. /**
  257. * Teardown the sync between scope and previous data object.
  258. */
  259. exports._unsyncData = function () {
  260. var listeners = this._syncListeners
  261. if (!listeners) {
  262. return
  263. }
  264. this.$observer
  265. .off('set:self', listeners.data.set)
  266. .off('add:self', listeners.data.add)
  267. .off('delete:self', listeners.data.delete)
  268. this._dataObserver
  269. .off('set:self', listeners.scope.set)
  270. .off('add:self', listeners.scope.add)
  271. .off('delete:self', listeners.scope.delete)
  272. this._syncListeners = null
  273. }