Skip to content

Instantly share code, notes, and snippets.

@Kilowhisky
Created May 15, 2017 22:31
Show Gist options
  • Save Kilowhisky/0bed6d64c042752c3d769b8387c5b098 to your computer and use it in GitHub Desktop.
Save Kilowhisky/0bed6d64c042752c3d769b8387c5b098 to your computer and use it in GitHub Desktop.
HasMany Unload Exception
import DS from 'ember-data';
import Ember from 'ember';
export default DS.RESTAdapter.extend({
namespace: 'api'
});
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 { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
asset: belongsTo('asset'),
order: attr('number')
});
import Ember from 'ember';
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
locations: hasMany('asset-location', { async: false }),
locationsSorted: Ember.computed.sort('locations','locationsSortedBy'),
locationsSortedBy: ['order:asc']
});
import Ember from 'ember';
export default Ember.Route.extend({
locations: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
init(){
$.mockjax({ url: '/api/assetLocations/0', responseText: { id: '0', asset: '1', order: '0' }});
$.mockjax({ url: '/api/assetLocations/1', responseText: { id: '1', asset: '2', order: '1' }});
$.mockjax({ url: '/api/assetLocations/2', responseText: { id: '2', asset: '3', order: '2' }});
$.mockjax({ url: '/api/assetLocations/3', responseText: { id: '3', asset: '1', order: '3' }});
$.mockjax({ url: '/api/assetLocations/4', responseText: { id: '4', asset: '2', order: '4' }});
$.mockjax({ url: '/api/assetLocations/5', responseText: { id: '5', asset: '3', order: '5' }});
$.mockjax({ url: '/api/assetLocations/6', responseText: { id: '6', asset: '1', order: '6' }});
$.mockjax({ url: '/api/assetLocations/7', responseText: { id: '7', asset: '2', order: '7' }});
$.mockjax({ url: '/api/assetLocations/8', responseText: { id: '8', asset: '3', order: '8' }});
$.mockjax({ url: '/api/assetLocations/9', responseText: { id: '9', asset: '1', order: '9' }});
$.mockjax({ url: '/api/assetLocations/10', responseText: { id: '10', asset: '2', order: '10' }});
$.mockjax({
url: '/api/assets',
responseText: [
{ id: '1' },
{ id: '2' },
{ id: '3' }
]
});
},
model(){
return this.store.findAll('asset')
},
actions: {
loadMoreLocation(){
this.store.findRecord('asset-location', this.locations.pop());
},
unloadLocation(location){
this.store.unloadRecord(location);
}
}
});
import DS from 'ember-data';
import Ember from 'ember';
export default DS.JSONSerializer.extend({});
<h1>Welcome to {{appName}}</h1>
<br>
{{outlet}}
<br>
Asset Records:
{{#each model as |asset|}}
<div>Asset {{asset.id}}:</div>
<div>Locations:
<ol>
{{#each asset.locationsSorted as |location|}}
<li>{{location.id}} <input type="button" value="Unload" {{action 'unloadLocation' location}} /></li>
{{/each}}
</ol>
</div>
{{/each}}
<input type="button" value="Load More Locations" {{action 'loadMoreLocation'}} />
{
"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",
"jquery-mockjax": "https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.js"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment