Last active
December 7, 2016 03:48
-
-
Save WenInCode/5596716e1499fb3cb86c77806222800e to your computer and use it in GitHub Desktop.
computed variations example
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 Ember from 'ember'; | |
const { | |
computed, | |
get, | |
} = Ember; | |
export default Ember.Component.extend({ | |
things: [], | |
thingCount: computed('things.[]', function() { | |
let things = get(this, 'things'); | |
return things.length; | |
}), | |
thingCountWithoutBrace: computed('things', function() { | |
let things = get(this, 'things'); | |
return things.length; | |
}), | |
actions: { | |
addThing() { | |
let things = get(this, 'things'); | |
things.pushObject({name: 'thing'}); | |
}, | |
}, | |
}); |
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 Ember from 'ember'; | |
const { | |
computed, | |
get, | |
set, | |
} = Ember; | |
export default Ember.Component.extend({ | |
thing: [ | |
{ name: 'harold', count: 12 }, | |
{ name: 'tim', count: 1 }, | |
{ name: 'Chaaaaad!!', count: -12 }, | |
], | |
thingCounts: computed('thing.@each.count', function() { | |
let thing = get(this, 'thing'); | |
let counts = thing.map((obj) => { | |
return get(obj, 'count'); | |
}); | |
return counts.toString(); | |
}), | |
thingCountsWithoutEach: computed('thing.[]', function() { | |
let thing = get(this, 'thing'); | |
let counts = thing.map((obj) => { | |
return get(obj, 'count'); | |
}); | |
return counts.toString(); | |
}), | |
actions: { | |
incrementCount(index) { | |
let thing = get(this, 'thing'); | |
let th = thing.objectAt(index); | |
let thCount = get(th, 'count'); | |
set(th, 'count', thCount += 1); | |
}, | |
}, | |
}); |
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 Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle' | |
}); |
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.10.6", | |
"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.9.0", | |
"ember-data": "2.9.0", | |
"ember-template-compiler": "2.9.0", | |
"ember-testing": "2.9.0" | |
}, | |
"addons": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment