Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save runspired/9cd0677596ed554f6bb7bb1395065e87 to your computer and use it in GitHub Desktop.
Save runspired/9cd0677596ed554f6bb7bb1395065e87 to your computer and use it in GitHub Desktop.
ember-1.13-over-rendering
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'input',
type: 'checkbox',
onChange: null,
attributeBindings: [ 'type', 'name', 'checked:checked' ],
change: function() {
this.sendAction('onChange', this.$().is(':checked'));
},
});
import Ember from 'ember';
const {
A,
set,
PromiseProxyMixin,
ArrayProxy,
computed
} = Ember;
const animals = 'https://raw.githubusercontent.com/boennemann/animals/master/words.json'
const PromiseArray = ArrayProxy.extend(PromiseProxyMixin);
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
term: '',
selection: computed(function() { return new A(); }),
items: computed(function() {
return PromiseArray.create({
promise: fetch(animals).then(res => res.json())
});
}),
selectableItems: computed('items.[]', function() {
const selection = this.get('selection');
return this.get('items').map(item => ({
item,
isSelected: selection.includes(item),
}));
}),
filteredItems: computed('selectableItems', 'term', function() {
const term = this.get('term');
const items = this.get('selectableItems');
if (!term) { return items; }
return items.filter(item => item.item.indexOf(term) !== -1);
}),
actions: {
updateSelection(item, isSelected) {
const selection = this.get('selection');
set(item, 'isSelected', isSelected);
isSelected ? selection.addObject(item.item)
: selection.removeObject(item.item);
},
clearSelection() {
this.get('selection').clear();
}
}
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
p {
background:papayawhip;
padding:15px;
height:30px;
}
ol {
list-style:none;
padding:0;
}
li {
border-left:2px solid rebeccapurple;
transition:0.5s border-left-width ease-in-out;
}
li:hover {
border-left-width:10px;
}
{{input value=term}}
Showing {{filteredItems.length}} of {{items.length}}
<button {{action "clearSelection"}}>Clear Selection</button>
<p>
<small>{{#each selection as |item|}} {{item}} {{/each}}</small>
</p>
<ol>
{{#each filteredItems as |item|}}
<li><label>
{{checkbox-input
name=item.item
checked=item.isSelected
onChange=(action "updateSelection" item)}}
{{item.item}}
</label></li>
{{/each}}
</ol>
{
"version": "0.10.5",
"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.8.0",
"ember-data": "2.8.0",
"ember-template-compiler": "2.8.0",
"ember-testing": "2.8.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment