Skip to content

Instantly share code, notes, and snippets.

@corrspt
Last active August 3, 2018 15:08
Show Gist options
  • Select an option

  • Save corrspt/8f898c29d661366761a1f2f5bed84fe1 to your computer and use it in GitHub Desktop.

Select an option

Save corrspt/8f898c29d661366761a1f2f5bed84fe1 to your computer and use it in GitHub Desktop.
Show Ember Data Issue?
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
currentParameter: null,
init() {
let wps = this.store.createRecord('welding-procedure-specification');
this.set('wps', wps);
},
actions: {
addParameterBadOnMyApp() {
let parameters = this.get('wps').get('weldingParameters');
let max = 0;
parameters.forEach((p) => {
if (p.get('orderNum') > max) {
max = p.get('orderNum');
}
});
let bs = this.store.createRecord('base-material', {
name: 'Test'
});
let newParameter = this.store.createRecord('wps-welding-parameter', {
passNumber: max + 1,
orderNumber: max + 1,
wps: this.wps,
baseMaterial: bs
});
parameters.pushObject(newParameter);
this.set('currentParameter', newParameter);
},
addParameterGoodOnMyApp() {
let parameters = this.get('wps').get('weldingParameters');
let max = 0;
parameters.forEach((p) => {
if (p.get('orderNum') > max) {
max = p.get('orderNum');
}
});
let newParameter = this.store.createRecord('wps-welding-parameter');
newParameter.set('passNumber', max + 1);
newParameter.set('orderNumber', max + 1);
newParameter.set('wps', this.wps);
parameters.pushObject(newParameter);
this.set('currentParameter', newParameter);
},
}
});
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 Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
wpsNumber: attr('string'),
weldingParameters: hasMany('wps-welding-parameter'),
baseMaterial: belongsTo('base-material')
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
passNumber: attr('number'),
orderNum: attr('number')
});
<h1>Issue description</h1>
<p>If you click on the button Add Parameter (Bad or Good) it will add an instance of `wps-welding-parameter` to the hasMany relation of the current `wps` instance and set that same instance of `wps-welding-parameter` to the "currentParameter" of the controller</p>
<p>In the template I then list all parameters in the hasMany relation and afterwards, the currentEditing parameter (if you click only once on the button, should be two lines, displaying the same toString info)</p>
<p>When you click the any button in this template you will get two lines printing the same "ember model instance"</p>
<p>The issue is that when I click a very similar "Add Parameter" in my app I seem to get two different instances of the model (see get the result displayed in the picture at the end of the template)</p> But there's no reference to it in the Ember Data store (via store.peekAll) or via the Ember Inspector. THis happened after an upgrade from Ember/Data 2.18 to Ember 3.2/Ember Data 3.1<br>
<br>
List of Parameters: <br>
{{#each wps.weldingParameters as |parameter|}}
{{parameter}}
{{/each}}
<br>
Current Parameter:
{{currentParameter}}
<br>
<hr>
<button {{action 'addParameterBadOnMyApp'}}> Add parameter Bad </button>
<pre> The code for this action 'addParameterBadOnMyApp' is very similar to what I had in my App </pre>
<button {{action 'addParameterGoodOnMyApp'}}> Add parameter Good </button>
<pre> The code for this action 'addParameterGoodOnMyApp' is how I was able to fix the issue on my app </pre>
<hr>
<br>
This is what happens on my App:
<img src='https://preview.ibb.co/hf77bK/image.png' />
{
"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"
},
"addons": {
"ember-data": "3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment