-
-
Save tasha-urbancic/359e6ec651cbe44a5c81ee68571fc4c4 to your computer and use it in GitHub Desktop.
new
This file contains 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 Component from '@ember/component'; | |
import EmberObject, { computed } from '@ember/object'; | |
import { alias } from '@ember/object/computed'; | |
export default Component.extend({ | |
dynamicModelProperty: computed('model', 'subpath', function() { | |
let subpath = this.subpath; | |
if (subpath) { | |
let DynamicClass = EmberObject.extend({ | |
female: alias(`model.${subpath}.female`) | |
}); | |
return DynamicClass.create({ model: this.model }); | |
} | |
return EmberObject.create({}); | |
}), | |
verbose: computed('dynamicModelProperty.female', function() { | |
return ( | |
`watching ${this.subpath} with value: ${this.dynamicModelProperty.value} -- known model` + | |
JSON.stringify(this.model) | |
); | |
}) | |
}); |
This file contains 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'; | |
export default Controller.extend({ | |
appName: 'Dynamic keys', | |
animals: { | |
chicken: { | |
male: 20, | |
female: 10 | |
}, | |
cow: { | |
male: 200, | |
female: 100, | |
}, | |
pig: { | |
male: 10, | |
female: 30 | |
} | |
}, | |
actions: { | |
incrementAnimal(type) { | |
this.set(`animals.${type}.female`, this.get(`animals.${type}.female`) + 1); | |
console.log('updated model', this.animals); | |
} | |
} | |
}); |
This file contains 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