Skip to content

Instantly share code, notes, and snippets.

@matthias-k
Created November 29, 2017 13:37
Show Gist options
  • Save matthias-k/c801b46983b761cfdaf9848f421bfc64 to your computer and use it in GitHub Desktop.
Save matthias-k/c801b46983b761cfdaf9848f421bfc64 to your computer and use it in GitHub Desktop.
Nested Dependencies
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
recordings: [{
id: 'recording1',
recordingParts: ['a', 'b'],
}, {
id: 'recording2',
recordingParts: ['c', 'd'],
}],
recordingParts: Ember.computed('recordings.@each.recordingParts', function() {
const recordingParts = this.get('recordings').map(recording => Ember.get(recording, 'recordingParts'));
console.log("New RecParts", recordingParts);
return recordingParts;
}),
flatRecordingParts: Ember.computed('recordingParts.[]', function() {
console.log("new flat rec Parts");
return this.get('recordingParts').toArray().reduce((a, b) => a.concat(b), []);
}),
interestingProperty: Ember.computed('flatRecordingParts', function() {
console.log("update interesting stuff");
// without getting flatRecordingParts, interesting Property is not
// updated although I don't need it's actual value here here.
let foobar = this.get('flatRecordingParts');
return this.get('recordings');
}),
actions: {
buttonClicked() {
console.log("Click");
const firstRecording = this.get('recordings')[0];
Ember.set(firstRecording, 'recordingParts', ['e', 'f']);
},
},
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
Recordings: {{recordings}}<br>
{{!--flatRecordingParts: {{flatRecordingParts}}--}}
interestingStuff: {{interestingProperty}}
<br>
<button {{action "buttonClicked"}}>foobar</button>s
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment