Skip to content

Instantly share code, notes, and snippets.

@Kilowhisky
Last active July 26, 2018 23:17
Show Gist options
  • Save Kilowhisky/5bee61b69a3813524b1d83b319c4fc30 to your computer and use it in GitHub Desktop.
Save Kilowhisky/5bee61b69a3813524b1d83b319c4fc30 to your computer and use it in GitHub Desktop.
HasMany Explosion
import DS from 'ember-data';
export default DS.RESTAdapter.extend({});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { sort } from '@ember/object/computed';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
name: attr('string'),
positions: hasMany('position', { async: false }),
positionsSorted: sort('positions', 'positionsSortedBy'),
positionsSortedBy: ['utc:desc']
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
utc: attr('date'),
asset: belongsTo('asset', {async: true, inverse: 'positions'}),
});
import Ember from 'ember';
import { hash } from 'rsvp';
export default Ember.Route.extend({
init(){
this._super(...arguments);
},
model(){
// Push assets where no relationships are defined
this.store.pushPayload('asset',{
assets: [{ id: 1, name: 'My Great Asset', positions: [] }]
});
return hash({
assets: this.store.peekAll('asset'),
positions: this.store.peekAll('position')
});
},
actions: {
loadPositions(){
let date = Date.now();
this.store.pushPayload('position',{
positions: [{ id: date, utc: date, asset: 1 }]
});
},
cleanupPositions(){
const positions = this.store.peekAll('position').sortBy('utc').reverse();
let positionCount = positions.length;
while(positionCount > 1){
let record = positions.popObject();
this.store.unloadRecord(record);
positionCount--;
}
}
}
});
import DS from 'ember-data';
export default DS.RESTSerializer.extend({});
<h1>Ember Data HasMany Explosion</h1>
<h3>Loaded Assets</h3>
{{#each model.assets as |asset|}}
{{asset.name}}
<ul>
<!-- Change this line to asset.positions to see the undefined elements being populated -->
{{#each asset.positionsSorted as |pos|}}
<li>{{pos.utc}}</li>
{{/each}}
</ul>
{{/each}}
<h3>Loaded Positions</h3>
<ul>
{{#each model.positions as |pos|}}
<li>{{pos.utc}}</li>
{{/each}}
</ul>
<hr />
<p>
To reproduce the issue click 'Load Positions' a couple times and then click 'cleanup positions'. The console will then puke an error about not being able to update a watcher on null/undefined item. After this point if you try and 'Load Positions' it will be unable to do so and will continue to give the same undefined error.
</p>
<button type="button" {{action 'loadPositions'}}>Load Positions</button>
<button type="button" {{action 'cleanupPositions'}}>Cleanup Positions</button>
{
"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",
"jquery-mockjax": "https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.js"
},
"addons": {
"ember-data": "3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment