Skip to content

Instantly share code, notes, and snippets.

@urbany
Last active September 2, 2020 08:50
Show Gist options
  • Save urbany/26f68cde34e59c46c0c4b8c47d808c42 to your computer and use it in GitHub Desktop.
Save urbany/26f68cde34e59c46c0c4b8c47d808c42 to your computer and use it in GitHub Desktop.
Bug when sorting hasMany
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { sort } from '@ember/object/computed';
import { A } from '@ember/array';
export default class ApplicationController extends Controller {
appName = 'test'
get services() {
return this.model.services;
}
get sortedServices() {
return this.model.services.sortBy('name');
}
get sortedThroughBusinessServices() {
return this.model.business.services.sortBy('name');
}
_sortParams = ['name']
@sort('model.services', '_sortParams') sortedServicesComputed
@action addService() {
const business = this.model.business;
const name = Math.round(Math.random()) ? 'John' : 'Doe';
const service = this.store.createRecord('service', { name, business });
}
}
import Model, { hasMany } from '@ember-data/model';
/*
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
*/
export default class extends Model {
@hasMany('service', { inverse: 'business' }) services
}
import Model, { attr, belongsTo } from '@ember-data/model';
/*
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
*/
export default class extends Model {
@attr() name
@belongsTo('business', { inverse: 'services' }) business
}
import Route from '@ember/routing/route';
export default class extends Route {
async model() {
const business = this.store.createRecord('business');
const services = await business.services;
return { business, services: services };
}
}
<button {{on "click" this.addService}}>Add service</button>
<h3>unsortted ({{this.services.length}})</h3>
<ul>
{{#each this.services as |service|}}
<li>{{service.name}}</li>
{{/each}}
</ul>
<hr>
<h3>sorted (computed) ({{this.sortedServicesComputed.length}})</h3>
<ul>
{{#each this.sortedServicesComputed as |service|}}
<li>{{service.name}}</li>
{{/each}}
</ul>
<hr>
<h3>sorted through business ({{this.sortedThroughBusinessServices.length}})</h3>
<ul>
{{#each this.sortedThroughBusinessServices as |service|}}
<li>{{service.name}}</li>
{{/each}}
</ul>
<hr>
<h3>sorted ({{this.sortedServices.length}}) - always empty :(</h3>
<ul>
{{#each this.sortedServices as |service|}}
<li>{{service.name}}</li>
{{/each}}
</ul>
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.1",
"ember-data": "3.21.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment