Created
July 25, 2017 18:32
-
-
Save farisj/651b176978236c678e9833afafea4327 to your computer and use it in GitHub Desktop.
Indeterminate/Checked Inputs in Ember
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'; | |
import _ from "lodash"; | |
const { computed, get, set } = Ember; | |
export default Ember.Component.extend({ | |
items: [], | |
allItems: computed.alias('items'), | |
selectedItems: [], | |
allItemsSelected: computed('allItems.length', 'selectedItems.length', function(){ | |
return get(this, 'allItems').length === get(this, 'selectedItems').length; | |
}), | |
someItemsSelected: computed('allItems.length', 'selectedItems.length', function(){ | |
return get(this, 'selectedItems').length > 0; | |
}), | |
actions: { | |
toggleAllItems: function(){ | |
if (get(this, 'someItemsSelected')){ | |
set(this, 'selectedItems', []); | |
} else { | |
set(this, 'selectedItems', get(this, 'allItems')); | |
} | |
}, | |
selectItem: function(item){ | |
let selectedItems = get(this, 'selectedItems').toArray(); | |
if (selectedItems.includes(item)) { | |
set(this, 'selectedItems', _.without(selectedItems, item)); | |
} else { | |
set(this, 'selectedItems', _.union(selectedItems, [item])); | |
} | |
} | |
}, | |
}); |
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', | |
items: [ {name: 'milk'}, {name: 'eggs'}, {name: 'cheese'} ] | |
}); |
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.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", | |
"ember-composable-helpers": "1.1.3", | |
"ember-lodash": "4.17.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment