Guest User

Untitled

a guest
Nov 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. Vue.component('pricing', {
  2.  
  3. template: '#pricing-row',
  4.  
  5. props: ['item'],
  6.  
  7. mounted() {
  8. this.addWatchers()
  9. },
  10.  
  11. methods: {
  12.  
  13. resetWatchers() {
  14. setTimeout(()=> {
  15. this.addWatchers()
  16. }, 700)
  17. },
  18.  
  19. addWatchers() {
  20.  
  21. this.updateNet = this.$watch(
  22. function() {
  23. return this.item.net
  24. },
  25. function() {
  26. // unmount other watchers
  27. this.updateMargin()
  28. this.updateSell()
  29. // calculate sell price and update
  30. this.setSellPrice()
  31. // re-add watchers
  32. this.resetWatchers()
  33. }
  34. ),
  35.  
  36. this.updateMargin = this.$watch(
  37. function() {
  38. return this.item.margin
  39. },
  40. function() {
  41. // unmount other watchers which can cause bounce effect
  42. this.updateSell()
  43. // calculate sell price and update
  44. this.setSellPrice()
  45. // re-add watchers
  46. this.resetWatchers()
  47. }
  48. ),
  49.  
  50. this.updateSell = this.$watch(
  51. function() {
  52. return this.item.sell
  53. },
  54. function(sellPrice) {
  55. // unmount other watchers which can cause bounce effect
  56. this.updateMargin()
  57. // update margin
  58. this.setMargin(sellPrice)
  59. // re-add watchers
  60. this.resetWatchers()
  61. }
  62. )
  63. },
  64.  
  65. setSellPrice() {
  66. let price = (100 / (100 - this.item.margin)) * this.item.net
  67. this.item.sell = price.toFixed(2)
  68. },
  69.  
  70. setMargin(sellPrice) {
  71. let profit = (sellPrice - this.item.net)
  72. let price = (100 * profit) / sellPrice
  73. this.item.margin = price.toFixed(2)
  74. }
  75. }
  76.  
  77. })
  78.  
  79. new Vue({
  80. el: '#vue',
  81. data: {
  82. prices: [
  83. {
  84. id: 1,
  85. net: 5,
  86. margin: 10,
  87. sell: 5.56
  88. },
  89. {
  90. id: 2,
  91. net: 7,
  92. margin: 10,
  93. sell: 7.78
  94. },
  95. ]
  96. }
  97. })
Add Comment
Please, Sign In to add comment