Skip to content

Instantly share code, notes, and snippets.

@felixkiss
Last active November 5, 2015 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixkiss/9857fd830a837866f641 to your computer and use it in GitHub Desktop.
Save felixkiss/9857fd830a837866f641 to your computer and use it in GitHub Desktop.
ember-filter-async-relationship
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle',
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
User: {{model.username}}
<br>
<br>
{{#each model.indexFields as |field|}}
{{field.label}}: {{field.value}}
{{/each}}
import DS from 'ember-data';
export default DS.Model.extend({
order: DS.attr('number'),
tab: DS.belongsTo('custom-field-tab', {async: true})
});
import DS from 'ember-data';
export default DS.Model.extend({
slug: DS.attr('string'),
label: DS.attr('string'),
icon: DS.attr('string'),
order: DS.attr('number')
});
import DS from 'ember-data';
export default DS.Model.extend({
label: DS.attr('string'),
positions: DS.hasMany('custom-field-position', {async: false})
});
import DS from 'ember-data';
export default DS.Model.extend({
value: DS.attr('string'),
field: DS.belongsTo('custom-field', {async: false})
});
import DS from 'ember-data';
const User = DS.Model.extend({
username: DS.attr('string'),
customFields: DS.hasMany('user-custom-field', {async: false}),
indexFields: function() {
return this._filterByTab(1);
}.property('customFields.[]'),
contactFields: function() {
return this._filterByTab(2);
}.property('customFields.[]'),
_filterByTab: function(tab) {
return this.get('customFields').filter(function(field) {
var position = field.get('field.positions').findBy('tab.id', '' + tab);
if (! position) {
return false;
}
field.order = position.get('order');
return true;
}).sortBy('order');
}
});
return User;
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.find('user', 1);
}
});
{
"version": "0.4.16",
"EmberENV": {
"FEATURES": {}
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.1.0",
"ember-data": "2.1.0",
"ember-template-compiler": "2.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment