Skip to content

Instantly share code, notes, and snippets.

@pablobm
Last active March 30, 2016 17:27
Show Gist options
  • Save pablobm/e820604c62b94a5828d6890d2e37efe2 to your computer and use it in GitHub Desktop.
Save pablobm/e820604c62b94a5828d6890d2e37efe2 to your computer and use it in GitHub Desktop.
Delete records with errors
import Ember from 'ember';
export default Ember.Controller.extend({
init() {
this.store.createRecord('line', {name: "yksi"});
this.store.createRecord('line', {name: "kaksi"});
this.store.createRecord('line', {name: "kolme"});
},
lines: Ember.computed(function() {
return this.store.findAll('line');
}),
actions: {
invalidate(line) {
// No problem if I use this to add the error
//line.get('errors')._add('name', "is not up to scratch");
// Recklessly using private APIs in order
// to reproduce the problem with minimum code
this.store.recordWasInvalid(
line._internalModel, {
name: "is not up to scratch",
}
);
},
delete(line) {
line.deleteRecord();
},
},
});
import DS from 'ember-data';
export default DS.Model.extend({
line: DS.attr('string'),
});
<h1>Why can't I delete records with errors?</h1>
<p>Trying to isolate a strange behaviour I'm observing in Ember Data.</p>
<p>To reproduce in an app, first create a record on the store. From there, the behaviour of <code>deleteRecord</code> varies depending on whether <code>save</code> is called on the record or not. Presently:</p>
<ul>
<li>No attempt to <code>save</code>: removes the record form the store</li>
<li>After a successful <code>save</code>: removes the record from the store</li>
<li>After a failed <code>save</code>: apparently nothing</li>
</ul>
<p>Records in the store:</p>
<ul>
{{#each lines as |line|}}
<li>
{{line.name}}
{{#if line.errors}}(<span style="color: red;">invalid</span>){{/if}}
-
<button {{action 'invalidate' line}}>make invalid</button>
-
<button {{action 'delete' line}}>delete</button>
</li>
{{/each}}
</ul>
{
"version": "0.7.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": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.3/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.4.3/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.3/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment