Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. import Ember from 'ember';
  2. import {
  3. observer,
  4. setProperties,
  5. set,
  6. get,
  7. computed
  8. } from '@ember/object';
  9.  
  10. export default Ember.Controller.extend({
  11. appName: 'Ember Twiddle',
  12. tableData: computed(function() {
  13. return []
  14. }),
  15. testData: computed(function() {
  16. return []
  17. }),
  18. method7Data: computed(function() {
  19. return []
  20. }),
  21.  
  22. setInitialTableData: function() {
  23. let arrOfObj = [
  24. {
  25. content: "This hasn't been changed, good.",
  26. method: "None",
  27. },
  28. {
  29. content: "Again, hasn't been changed, good.",
  30. method: "None",
  31. }
  32. ];
  33.  
  34. set(this, 'tableData', arrOfObj);
  35. set(this, 'method7Data', arrOfObj);
  36. },
  37.  
  38. init() {
  39. this.setInitialTableData();
  40. },
  41.  
  42. actions: {
  43. // Methods Of trying not to change the initial tableData property.
  44. method1() {
  45. let newArr = get(this, 'tableData');
  46.  
  47. newArr.forEach((newRow) => {
  48. set(newRow, 'content', 'ITS CHANGED NOOO!!!');
  49. set(newRow, 'method', 'Method 1, normal get.');
  50. });
  51. },
  52.  
  53. method2() {
  54. let newArr = new Array(...get(this, 'tableData'));
  55.  
  56. newArr.forEach((newRow) => {
  57. set(newRow, 'content', 'ITS CHANGED NOOO!!!');
  58. set(newRow, 'method', 'Method 2, new Array(destructured get).');
  59. });
  60. },
  61.  
  62. method3() {
  63. let newArr = [...get(this, 'tableData')];
  64.  
  65. newArr.forEach((newRow) => {
  66. set(newRow, 'content', 'ITS CHANGED NOOO!!!');
  67. set(newRow, 'method', 'Method 3, destructured get');
  68. });
  69. },
  70.  
  71. method4() {
  72. let arr1 = [];
  73. let arr2 = get(this, 'tableData');
  74. let newArr = arr1.concat(arr2);
  75.  
  76. newArr.forEach((newRow) => {
  77. set(newRow, 'content', 'ITS CHANGED NOOO!!!');
  78. set(newRow, 'method', 'Method 4, using concat.');
  79. });
  80. },
  81.  
  82. method5() {
  83. let newArr = get(this, 'tableData');
  84.  
  85. newArr.forEach((newRow) => {
  86. set(newRow, 'content', 'Tried a simple set using =, causes "you must use set" error!!!');
  87. set(newRow, 'method', 'Method 5, check console.');
  88. newRow.content = 'ITS CHANGED NOOO!!!';
  89. });
  90. },
  91.  
  92. method6() {
  93. set(this, 'testData', get(this, 'tableData'));
  94. let newArr = get(this, 'testData');
  95.  
  96. newArr.forEach((newRow) => {
  97. set(newRow, 'content', 'ITS CHANGED NOOOO!!');
  98. set(newRow, 'method', 'Method 6, created a new computed property from tableData.');
  99. });
  100. },
  101.  
  102. method7() {
  103. let newArr = get(this, 'method7Data');
  104.  
  105. newArr.forEach((newRow) => {
  106. set(newRow, 'content', 'ITS CHANGED NOOOO!!');
  107. set(newRow, 'method', 'Method 7, pulling from a completely different property.');
  108. });
  109. },
  110.  
  111. method8() {
  112. let originalArray = get(this, 'tableData');
  113. let newArr = this.send('_manipulateData', originalArray);
  114.  
  115. console.log(newArr);
  116. },
  117.  
  118. _manipulateData(originalArray) {
  119. return originalArray.forEach((newRow) => {
  120. set(newRow, 'content', 'ITS CHANGED NOOO!!!');
  121. set(newRow, 'method', 'Method 8, using another action to manipulate the data.');
  122. });
  123. },
  124.  
  125. // Method 9 works!
  126. method9() {
  127. let newArr = get(this, 'tableData');
  128. let exportData = [];
  129.  
  130. newArr.forEach((newRow) => {
  131. let content = newRow.content.toUpperCase();
  132. let method = newRow.method.toUpperCase();
  133.  
  134. exportData.push({
  135. content: content,
  136. method: method
  137. });
  138. });
  139.  
  140. console.log(exportData);
  141. },
  142. }
  143. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement