Last active
February 7, 2020 17:37
-
-
Save ASH-Michael/ad3078e054874df79bdfeb1f2089de33 to your computer and use it in GitHub Desktop.
Overwriting Computed Properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Controller from '@ember/controller'; | |
import { computed, set } from '@ember/object'; | |
import { alias, oneWay } from '@ember/object/computed'; | |
export default Controller.extend({ | |
// demo 1 | |
color1: 'red', | |
color2: 'blue', | |
color3: computed('color1', 'color2', function() { | |
const same = this._checkIfSame(this.color1, this.color2); | |
return same ? this.color1 : this._getSecondaryColor(this.color1, this.color2); | |
}), | |
// demo 2 | |
color4: 'red', | |
color5: 'blue', | |
color4oneWay: oneWay('color4'), | |
color6: computed('color4', 'color5', function() { | |
const same = this._checkIfSame(this.color4, this.color5); | |
return same ? this.color4 : this._getSecondaryColor(this.color4, this.color5); | |
}), | |
// demo 3 | |
color7: 'red', | |
color8: 'blue', | |
color7alias: alias('color7'), | |
color9: computed('color7', 'color8', function() { | |
const same = this._checkIfSame(this.color7, this.color8); | |
return same ? this.color7 : this._getSecondaryColor(this.color7, this.color8); | |
}), | |
// demo 4 | |
color10: 'red', | |
color11: 'blue', | |
colorObject: computed('color10', 'color11', function() { | |
const same = this._checkIfSame(this.color10, this.color11); | |
return { | |
primary1: this.color10, | |
primary2: this.color11, | |
secondary: same ? this.color10 : this._getSecondaryColor(this.color10, this.color11) | |
}; | |
}), | |
_checkIfSame(color1, color2) { | |
return color1 === color2; | |
}, | |
_getSecondaryColor(primary1, primary2) { | |
const primaryColors = [primary1, primary2]; | |
if (primaryColors.includes('red') && primaryColors.includes('blue')) { | |
return 'purple'; | |
} | |
if (primaryColors.includes('red') && primaryColors.includes('yellow')) { | |
return 'orange'; | |
} | |
if (primaryColors.includes('blue') && primaryColors.includes('yellow')) { | |
return 'green'; | |
} | |
}, | |
actions: { | |
changeColor(property, color) { | |
set(this, property, color); | |
}, | |
changeColorObject(color) { | |
set(this, 'colorObject.secondary', color); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.15.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", | |
"ember": "3.4.3", | |
"ember-template-compiler": "3.4.3", | |
"ember-testing": "3.4.3" | |
}, | |
"addons": { | |
"ember-data": "3.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment