Skip to content

Instantly share code, notes, and snippets.

@vasilionjea
Created September 25, 2018 22:16
Show Gist options
  • Save vasilionjea/1b4d97947a83abee828958015dd89c68 to your computer and use it in GitHub Desktop.
Save vasilionjea/1b4d97947a83abee828958015dd89c68 to your computer and use it in GitHub Desktop.
Computed Property for "wrapped items"
import Ember from 'ember';
export default Ember.Controller.extend({
canCreate: true,
hasMax: true,
items: [
{ isEnabled: true, name: 'duplicate'},
{ isEnabled: true, name: 'edit'},
],
renderedItems: Ember.computed('items.[]', 'canCreate', 'hasMax', function() {
const { items, canCreate, hasMax } = this.getProperties('items', 'canCreate', 'hasMax');
return items.map(item => {
const canNotDuplicate = item.name === 'duplicate' && hasMax;
const canNotEdit = item.name === 'edit' && !canCreate;
const wrappedItem = Object.assign({
isDisabled: (!item.isEnabled || canNotDuplicate || canNotEdit)
}, item);
return wrappedItem;
});
}),
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
ul, li {
margin: 0;
padding: 0;
list-style: none;
}
li {
display: flex;
align-items: center;
justify-content: space-between;
padding: 1rem;
border: 1px solid #c8c8c8;
margin-bottom: .5rem;
}
{{outlet}}
<h1>CP for "wrapped items"</h1>
<ul>
{{#each renderedItems as |item|}}
<li>{{item.name}} <small>isDisabled = {{item.isDisabled}}</small></li>
{{/each}}
</ul>
{
"version": "0.15.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment