Skip to content

Instantly share code, notes, and snippets.

@locks
Forked from pablobm/adapters.line.js
Created March 26, 2016 00:42
Show Gist options
  • Save locks/466712399c91c26c4ac9 to your computer and use it in GitHub Desktop.
Save locks/466712399c91c26c4ac9 to your computer and use it in GitHub Desktop.
Updates, errors, and the store
import Ember from 'ember';
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
ajax(url, type, options) {
const requestData = {
url: url,
method: type
};
const payload = {
errors: [
{
detail: 'KPOW!',
source: {
pointer: 'data/attributes/name',
},
},
],
};
// This line to FAIL
const response = this.handleResponse(422, {}, payload, response);
// This line to SUCCEED
//const response = this.handleResponse(200, {}, {}, response);
return new Ember.RSVP.Promise(function(resolve, reject) {
Ember.run.join(null, reject, response);
}, "Dummy request");
},
});
import Ember from 'ember';
export default Ember.Controller.extend({
init() {
this._super();
this.store.createRecord('line', {name: 'yksi'});
this.store.createRecord('line', {name: 'kaksi'});
this.store.createRecord('line', {name: 'kolme'});
},
newLine: Ember.computed(function() {
return this.store.createRecord('line', {name: '...'});
}),
lines: Ember.computed(function() {
return this.store.findAll('line');
}),
actions: {
save() {
const line = this.get('newLine');
line.save().then(
function() {},
function() { line.deleteRecord(); },
);
},
cancel() {
this.get('newLine').deleteRecord();
},
},
});
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
this.store.createRecord('line', {name: 'yksi'});
this.store.createRecord('line', {name: 'kaksi'});
this.store.createRecord('line', {name: 'kolme'});
return {
lines: this.store.peekAll('line'),
newLine: this.store.createRecord('line', {name: '...'})
};
}
});
<h1>Updates, errors, and the store</h1>
<h2>What is this?</h2>
<p>Demo of something confusing I'm seeing on Ember Data.</p>
<p>Create a record on the store. The behaviour of `deleteRecord` varies depending on whether `save` is called on the record or not. Presently:</p>
<ul>
<li>No attempt to `save`: removes the record form the store</li>
<li>After a successful `save`: removes the record from the store</li>
<li>After a failed `save`: apparently nothing</li>
</ul>
<p>This example uses a custom adapter that performs a dummy `save`. It will fail or succeed depending on two lines that can be commented/uncommented for experimentation.</p>
<h2>The example</h2>
<form>
<p>
Add a new line: {{input value=newLine.name}}
<button {{action 'save'}}>Add</button>
</p>
<p>{{newLine.errors.name.0.message}}</p>
<p><a href="#" {{action 'cancel'}}>Cancel</a></p>
</form>
<ul>
{{#each lines as |line|}}
<li>{{line.name}}</li>
{{/each}}
</ul>
{
"version": "0.7.0",
"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