Skip to content

Instantly share code, notes, and snippets.

@workmanw
Last active October 23, 2017 23:39
Show Gist options
  • Save workmanw/7cfe7142524edb3a03755fb913dc035f to your computer and use it in GitHub Desktop.
Save workmanw/7cfe7142524edb3a03755fb913dc035f to your computer and use it in GitHub Desktop.
ember-data#4918 / UnloadRecord State
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
store: Ember.inject.service('store'),
errorMsgs: null,
init() {
this._super(...arguments);
let store = this.get('store');
store.push({
data: [{
"type": "post",
"id": "1",
"attributes": {
"name": "JSON API paints my bikeshed!"
}
}]
});
this.set('post', store.peekRecord('post', 1));
this.set('errorMsgs', []);
Ember.onerror = (error) => {
this.get('errorMsgs').pushObject(error.message);
};
},
actions: {
dirty() {
this.get('post').set('name', 'dirty');
},
rollback() {
this.get('post').rollbackAttributes();
},
unload() {
this.get('post').unloadRecord();
}
}
});
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')
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.error-container {
color: #a94442;
background-color: #f2dede;
border-color: #ebccd1;
padding: 6px;
}
.error-container .message {
margin: 8px 0;
display: block;
font-size: 13px;
}
<h1>Welcome to {{appName}}</h1>
<br>
<br>
State: {{post.currentState.stateName}}
<br><br>
<button {{action "dirty"}}>Dirty Record</button>
<button {{action "rollback"}}>Rollback Record</button>
<button {{action "unload"}}>Unload Record</button>
<br><br><br>
{{#if errorMsgs}}
<div class="error-container">
{{#each errorMsgs as |msg|}}
<span class="message">{{msg}}</span>
{{/each}}
</div>
{{/if}}
{
"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.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment