Last active
July 5, 2017 17:48
-
-
Save alexdiliberto/3de32b1404d8852665ef7d7af2c44950 to your computer and use it in GitHub Desktop.
Ember 'Toggle All' Checkbox #2
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 Ember from 'ember'; | |
| var COLORS = 'peachpuff khaki papayawhip cadetblue orchid seashell gainsboro thistle olivedrab aquamarine azure crimson darkslategray goldenrod cornsilk chartreuse darksalmon firebrick honeydew'.w(); | |
| var ID = 4; | |
| export default Ember.ObjectController.extend({ | |
| toggles: function(){ return Ember.A([]) }.property(), | |
| allChecked: function(key, value){ | |
| if (arguments.length === 1) { | |
| var toggles = this.get('toggles'); | |
| return toggles && toggles.isEvery('isChecked') | |
| } else { | |
| this.get('toggles').setEach('isChecked', value); | |
| return value; | |
| } | |
| }.property('toggles.@each.isChecked'), | |
| allSelectedItems: Ember.computed.filterBy('toggles', 'isChecked', true), | |
| totalSelectedCount: Ember.computed.alias('allSelectedItems.length'), | |
| actions: { | |
| registerToggle: function(toggle) { | |
| this.get('toggles').addObject(toggle); | |
| }, | |
| deregisterToggle: function(toggle) { | |
| this.get('toggles').removeObject(toggle); | |
| }, | |
| add: function() { | |
| var color = COLORS[Math.floor(Math.random() * COLORS.length)]; | |
| this.get('model').pushObject({color: color, id: ID++}); | |
| }, | |
| remove: function() { | |
| var allSelectedItems = this.get('allSelectedItems').mapBy('content'); | |
| this.get('model').removeObjects(allSelectedItems); | |
| } | |
| } | |
| }); |
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 Ember from 'ember'; | |
| export default Ember.ObjectController.extend({ | |
| isChecked: false, | |
| colorId: function() { | |
| return 'checkbox' + this.get('id'); | |
| }.property('id'), | |
| registerOnParent: function() { | |
| this.send('registerToggle', this); | |
| }.on('init'), | |
| willDestroy: function() { | |
| this.send('deregisterToggle', this); | |
| } | |
| }); |
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 Ember from 'ember'; | |
| export default Ember.Route.extend({ | |
| model: function() { | |
| return Ember.A([ | |
| {color: 'red', id: 1}, | |
| {color: 'yellow', id: 2}, | |
| {color: 'blue', id: 3} | |
| ]); | |
| } | |
| }); |
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
| body { | |
| margin: 12px 16px; | |
| font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
| font-size: 12pt; | |
| } | |
| label, input[type=checkbox] { | |
| cursor: pointer; | |
| } |
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.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": "1.12.0", | |
| "ember-template-compiler": "1.12.0", | |
| "ember-testing": "1.12.0" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment