Skip to content

Instantly share code, notes, and snippets.

@fivetanley
Last active October 19, 2017 13:49
Show Gist options
  • Save fivetanley/1811e61d99eeff347f2067e30b41c23a to your computer and use it in GitHub Desktop.
Save fivetanley/1811e61d99eeff347f2067e30b41c23a to your computer and use it in GitHub Desktop.
ember data call create record response
import DS from 'ember-data';
export default DS.RESTAdapter.extend();
import Ember from 'ember';
let ids = 0;
$.mockjax({
type: 'POST',
url: '/posts',
responseText: function(data) {
console.log('data', data);
return {
post: {
'non-id-primary-key': ++ids,
}
}
}
});
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
allPosts: Ember.computed(function() {
return this.store.peekAll('post');
}),
actions: {
create() {
const title = this.get('title');
const record = this.store.createRecord('post', {
title
});
return record.save();
}
}
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
title: attr()
});
import DS from 'ember-data';
export default DS.RESTSerializer.extend({
primaryKey: 'non-id-primary-key',
serialize() {
alert('serialize called');
return this._super(...arguments);
},
normalizeCreateRecordResponse: function(store, modelClass, payload) {
alert('omg payload' + payload);
console.log('payload', payload);
debugger;
return this._super(...arguments);
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
<ul>
{{#each allPosts as |post|}}
<li>{{post.id}} - {{post.title}} - {{post.isNew}}</li>
{{/each}}
</ul>
title: {{input value=title}}
<button {{action "create"}}>Create Record</button>
{
"version": "0.11.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": "2.10.2",
"ember-data": "2.11.0",
"ember-template-compiler": "2.10.2",
"ember-testing": "2.10.2",
"mockjax": "https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.js"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment