inheritance.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var Vue = require('../src/vue')
  2. window.model = {
  3. a: 'parent a',
  4. b: 'parent b',
  5. c: {
  6. d: 3
  7. },
  8. arr: [{a: 'item a'}],
  9. go: function () {
  10. console.log(this.a)
  11. }
  12. }
  13. window.vm = new Vue({
  14. data: model
  15. })
  16. window.child = new Vue({
  17. parent: vm,
  18. data: {
  19. a: 'child a',
  20. change: function () {
  21. this.c.d = 4
  22. this.b = 456 // Unlike Angular, setting primitive values in Vue WILL affect outer scope,
  23. // unless you overwrite it in the instantiation data!
  24. }
  25. }
  26. })
  27. window.item = new Vue({
  28. parent: vm,
  29. syncData: true,
  30. data: vm.arr[0]
  31. })
  32. vm._observer.on('set', function (key, val) {
  33. console.log('vm set:' + key.replace(/[\b]/g, '.'), val)
  34. })
  35. child._observer.on('set', function (key, val) {
  36. console.log('child set:' + key.replace(/[\b]/g, '.'), val)
  37. })
  38. item._observer.on('set', function (key, val) {
  39. console.log('item set:' + key.replace(/[\b]/g, '.'), val)
  40. })
  41. // TODO turn these into tests
  42. console.log(vm.a) // 'parent a'
  43. console.log(child.a) // 'child a'
  44. console.log(child.$scope.a) // 'child a'
  45. console.log(child.b) // undefined
  46. console.log(child.$scope.b) // 'parent b'
  47. console.log(item.a) // 'item a'
  48. console.log(item.$scope.a) // 'item a'
  49. console.log(item.b) // undefined
  50. console.log(item.$scope.b) // 'parent b'
  51. // set shadowed parent property
  52. vm.a = 'haha!' // vm set:a haha!
  53. // set shadowed child property
  54. child.a = 'hmm' // child set:a hmm
  55. // test parent scope change downward propagation
  56. vm.b = 'hoho!' // child set:b hoho!
  57. // item set:b hoho!
  58. // vm set:b hoho!
  59. // set child owning an array item
  60. item.a = 'wow' // child set:arr.0.a wow
  61. // item set:arr.0.a wow
  62. // vm set:arr.0.a wow
  63. // item set:a wow