Skip to content

Instantly share code, notes, and snippets.

@mansona
Last active April 25, 2018 09:45
Show Gist options
  • Save mansona/f7e9fcdc4fe6f9205de63471a25e7fd1 to your computer and use it in GitHub Desktop.
Save mansona/f7e9fcdc4fe6f9205de63471a25e7fd1 to your computer and use it in GitHub Desktop.
May I ask a Question - 001
import Ember from 'ember';
export default Ember.Controller.extend({
filteredModel: Ember.computed('filterValue', 'model.@each.name', function() {
if(Ember.isEmpty(this.get('filterValue'))) {
return this.model;
}
return this.model.filter((test) => test.get('name') === this.get('filterValue'));
}),
actions: {
filterByTestName: function(name) {
this.set('filterValue', name);
}
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
});
import Ember from 'ember';
function makeTestRecord(context, name){
context.get('store').createRecord('test', {
name,
});
}
export default Ember.Route.extend({
model() {
makeTestRecord(this, 'first test');
makeTestRecord(this, 'first test');
makeTestRecord(this, 'face');
makeTestRecord(this, 'face');
makeTestRecord(this, 'face');
makeTestRecord(this, 'face');
makeTestRecord(this, 'last test');
return this.get('store').peekAll('test');
}
});
{{#each filteredModel as |test|}}
<br>
{{test.name}}
{{/each}}
<br>
<button {{action 'filterByTestName' 'face'}}>Face</button>
<button {{action 'filterByTestName' 'first test'}}>First Test</button>
<button {{action 'filterByTestName' ''}}>Clear</button>
<br>
<br>
Twiddle used to answer <a href="https://stackoverflow.com/questions/49930156/ember-modify-model-in-a-route">this Stack Overflow Question</a>
<br>
<br>
Full answer <a href="https://www.youtube.com/watch?v=gFs6dgqrgCw">recorded live for YouTube</a>
{
"version": "0.13.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.16.2",
"ember-template-compiler": "2.16.2",
"ember-testing": "2.16.2"
},
"addons": {
"ember-data": "2.16.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment