Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Ember from 'ember';
- import {
- observer,
- setProperties,
- set,
- get,
- computed
- } from '@ember/object';
- export default Ember.Controller.extend({
- appName: 'Ember Twiddle',
- tableData: computed(function() {
- return []
- }),
- testData: computed(function() {
- return []
- }),
- method7Data: computed(function() {
- return []
- }),
- setInitialTableData: function() {
- let arrOfObj = [
- {
- content: "This hasn't been changed, good.",
- method: "None",
- },
- {
- content: "Again, hasn't been changed, good.",
- method: "None",
- }
- ];
- set(this, 'tableData', arrOfObj);
- set(this, 'method7Data', arrOfObj);
- },
- init() {
- this.setInitialTableData();
- },
- actions: {
- // Methods Of trying not to change the initial tableData property.
- method1() {
- let newArr = get(this, 'tableData');
- newArr.forEach((newRow) => {
- set(newRow, 'content', 'ITS CHANGED NOOO!!!');
- set(newRow, 'method', 'Method 1, normal get.');
- });
- },
- method2() {
- let newArr = new Array(...get(this, 'tableData'));
- newArr.forEach((newRow) => {
- set(newRow, 'content', 'ITS CHANGED NOOO!!!');
- set(newRow, 'method', 'Method 2, new Array(destructured get).');
- });
- },
- method3() {
- let newArr = [...get(this, 'tableData')];
- newArr.forEach((newRow) => {
- set(newRow, 'content', 'ITS CHANGED NOOO!!!');
- set(newRow, 'method', 'Method 3, destructured get');
- });
- },
- method4() {
- let arr1 = [];
- let arr2 = get(this, 'tableData');
- let newArr = arr1.concat(arr2);
- newArr.forEach((newRow) => {
- set(newRow, 'content', 'ITS CHANGED NOOO!!!');
- set(newRow, 'method', 'Method 4, using concat.');
- });
- },
- method5() {
- let newArr = get(this, 'tableData');
- newArr.forEach((newRow) => {
- set(newRow, 'content', 'Tried a simple set using =, causes "you must use set" error!!!');
- set(newRow, 'method', 'Method 5, check console.');
- newRow.content = 'ITS CHANGED NOOO!!!';
- });
- },
- method6() {
- set(this, 'testData', get(this, 'tableData'));
- let newArr = get(this, 'testData');
- newArr.forEach((newRow) => {
- set(newRow, 'content', 'ITS CHANGED NOOOO!!');
- set(newRow, 'method', 'Method 6, created a new computed property from tableData.');
- });
- },
- method7() {
- let newArr = get(this, 'method7Data');
- newArr.forEach((newRow) => {
- set(newRow, 'content', 'ITS CHANGED NOOOO!!');
- set(newRow, 'method', 'Method 7, pulling from a completely different property.');
- });
- },
- method8() {
- let originalArray = get(this, 'tableData');
- let newArr = this.send('_manipulateData', originalArray);
- console.log(newArr);
- },
- _manipulateData(originalArray) {
- return originalArray.forEach((newRow) => {
- set(newRow, 'content', 'ITS CHANGED NOOO!!!');
- set(newRow, 'method', 'Method 8, using another action to manipulate the data.');
- });
- },
- // Method 9 works!
- method9() {
- let newArr = get(this, 'tableData');
- let exportData = [];
- newArr.forEach((newRow) => {
- let content = newRow.content.toUpperCase();
- let method = newRow.method.toUpperCase();
- exportData.push({
- content: content,
- method: method
- });
- });
- console.log(exportData);
- },
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement