Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save HeroicEric/e215126d6211cd13703e71156231b800 to your computer and use it in GitHub Desktop.
Save HeroicEric/e215126d6211cd13703e71156231b800 to your computer and use it in GitHub Desktop.
Awesome Macros Array Dependents
import Ember from 'ember';
import raw from 'ember-macro-helpers/raw';
import { filterBy } from 'ember-awesome-macros/array';
export default Ember.Controller.extend({
redItems: filterBy('data', raw('color'), raw('red')),
blueItems: Ember.computed('data.@each.color', function() {
return this.get('data').filterBy('color', 'blue');
}),
init() {
this._super(...arguments);
this.set('data', Ember.A());
},
actions: {
addItem(color) {
let data = this.get('data');
let id = data.length + 1;
data.pushObject({ id, color });
}
}
});
<h3>All items</h3>
{{#each data as |item|}}
<p style="color: white; background-color: {{item.color}}">
{{item.id}}
</p>
{{else}}
No items yet
{{/each}}
<h3>Filtered Red Items</h3>
{{#each redItems as |item|}}
<p style="color: white; background-color: {{item.color}}">
{{item.id}}
</p>
{{else}}
No red items yet
{{/each}}
<h3>Filtered Blue Items</h3>
{{#each blueItems as |item|}}
<p style="color: white; background-color: {{item.color}}">
{{item.id}}
</p>
{{else}}
No blue items yet
{{/each}}
<div style="margin-top: 1em">
<button {{action "addItem" "red"}}>Add Red Item</button>
<button {{action "addItem" "blue"}}>Add Blue Item</button>
</div>
{
"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-awesome-macros": "0.36.1",
"ember-macro-helpers": "0.15.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment