Skip to content

Instantly share code, notes, and snippets.

@runspired
Last active May 9, 2017 15:54
Show Gist options
  • Save runspired/c92c8d066511083f8c171a33ae27dedf to your computer and use it in GitHub Desktop.
Save runspired/c92c8d066511083f8c171a33ae27dedf to your computer and use it in GitHub Desktop.
Can we unload a record that has been deleted?
let IS_DELETED = false;
export default class Adapter {
queryRecord() {
let isDeleted = IS_DELETED;
IS_DELETED = true;
return Promise.resolve({
data: {
type: 'api-managed',
id: '1',
attributes: {
isDeleted
}
}
});
}
get() {
return null;
}
static create() {
return new Adapter();
}
}
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
hasUpdatedDeletedRecord: false,
actions: {
updateRecord() {
let isDeleted = this.get('record.isDeleted');
this.set('hasUpdatedDeletedRecord', isDeleted);
this.get('store').queryRecord('api-managed', {});
},
unloadRecord() {
let record = this.get('record.content');
this.set('record', null);
record.unloadRecord();
}
},
init() {
this._super();
this.record = this.get('store').queryRecord('api-managed', {});
}
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
isDeleted: attr()
});
export default class Serializer {
normalizeResponse(_, __, data) {
return data;
}
static create() {
return new Serializer();
}
}
<h1>Welcome to {{appName}}</h1>
<br>
{{#if record}}
My record is: {{if record.isDeleted 'deleted' 'not deleted'}}
{{else}}
<b>Record Deleted</b>
{{/if}}
{{#unless record.isDeleted}}
<button {{action "updateRecord"}}>Fetch Remote Deletion</button>
{{else}}
{{#unless hasUpdatedDeletedRecord}}
<button {{action "updateRecord"}}>Update Remote Deletion</button>
{{else}}
<button {{action "unloadRecord"}}>Unload Deleted Record</button>
{{/unless}}
{{/unless}}
<br>
{{outlet}}
<br>
<br>
{
"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"
},
"addons": {
"ember-data": "~2.13.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment