Skip to content

Instantly share code, notes, and snippets.

@Kilowhisky
Last active August 29, 2018 15:27
Show Gist options
  • Save Kilowhisky/9d09b6673cb026a0303bab0d1bbc6f0a to your computer and use it in GitHub Desktop.
Save Kilowhisky/9d09b6673cb026a0303bab0d1bbc6f0a to your computer and use it in GitHub Desktop.
Reload Record Hell
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 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({
people: hasMany('person'),
name: attr('string')
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
name: attr('string')
});
import Ember from 'ember';
import { hash } from 'rsvp';
export default Ember.Route.extend({
init(){
$.mockjax({ url: '/api/contracts/1', responseText: { id: '1', name: 'contract 1', people: [] }});
$.mockjax({
url: '/api/people',
responseText: [
{ id: '1', name: 'tom' },
{ id: '2', name: 'mary' },
{ id: '3', name: 'dale' }
]
});
},
model(){
return hash({
contract: this.store.findRecord('contract', '1'),
people: this.store.findAll('person')
});
},
actions: {
reload(contract, people){
contract.reload();
people.update();
},
rollback(contract){
contract.rollbackAttributes();
},
force(contract){
contract.hasMany('people').push([]);
},
associate(person, contract){
contract.people.addObject(person);
},
allTheThings(model){
model.contract.unloadRecord();
this.store.unloadAll('people');
this.refresh();
}
}
});
import DS from 'ember-data';
import Ember from 'ember';
export default DS.JSONSerializer.extend({});
<h1>Welcome to Record Reload Hell</h1>
<br>
{{outlet}}
<br>
<p>
This twiddle shows that when a hasMany record is reloaded the relationship state is not accepted as canonical.
</p>
<hr />
Editing contract {{model.contract.id}}
<div>
<label>Name</label>
{{input value=model.contract.name}}
</div>
<div>
People associated with contract:
<ul>
{{#each model.contract.people as |person|}}
<li>{{person.name}}</li>
{{/each}}
</ul>
</div>
<hr />
People available for association:
<ul>
{{#each model.people as |person|}}
<li>{{person.name}} <button type="button" {{action 'associate' person model.contract}}>Associate</button></li>
{{/each}}
</ul>
<button type="button" {{action 'reload' model.contract model.people}}>Reload the record</button>
<button type="button" {{action 'rollback' model.contract}}>Rollback the attributes</button>
<button type="button" {{action 'force' model.contract}}>Force HasMany Canonical</button>
<button type="button" {{action 'allTheThings' model}}>RELOAD ALL THE THINGS</button>
<p>
None of these buttons do anythign except for the last one and that is an extreme case that cannot really be used in production code
</p>
{
"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": "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",
"ember-power-select": "2.0.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment