Skip to content

Instantly share code, notes, and snippets.

@miguelcobain
Created February 4, 2016 14:54
Show Gist options
  • Save miguelcobain/2b4af724b5f61ba06cf4 to your computer and use it in GitHub Desktop.
Save miguelcobain/2b4af724b5f61ba06cf4 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
var categories = [
{
id: 1,
title: 'category 1'
}, {
id: 2,
title: 'category 2'
}, {
id: 3,
title: 'category 3'
}];
export default Ember.Controller.extend({
categories: categories,
selectedCategories: Ember.K,
actions: {
selectedChanged(selected) {
this.set('selectedCategories', selected);
}
}
});
<h1> List Group Disaster </h1>
<h2> select category </h2>
{{#list-group model=categories selectedChanged=(action "selectedChanged") as |lg m|}}
{{lg.item text=m.title}}
{{/list-group}}
<h2> selected categories: </h2>
{{#list-group model=selectedCategories as |lg m|}}
{{lg.item text=m.title}}
{{/list-group}}
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'li',
classNames: ['list-group-item'],
classNameBindings: ['isActive:active'],
isActive: false,
click() {
this.toggleProperty('isActive');
this.get('selectedChanged')(this.get('isActive'));
}
});
{{#if hasBlock}}
{{yield}}
{{else}}
{{text}}
{{/if}}
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'ul',
classNames: ['list-group'],
selected: Ember.A(),
actions: {
itemSelected(model, isSelected) {
let selected = this.get('selected');
if (isSelected) {
selected.addObject(model);
} else {
selected.removeObject(model);
}
//test if `this.get('selectedChanged')` exists
//before invoking it
this.get('selectedChanged')(selected);
}
}
});
{{#each model as |m|}}
{{yield (hash
item=(component "list-group-item"
selectedChanged=(action "itemSelected" m)
)
) m}}
{{/each}}
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.active {
background-color: red;
}
{
"version": "0.5.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "beta",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.2.0/ember-data.js",
"ember-template-compiler": "beta"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment