Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extended Models with custom adapter and serializer
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
namespace: 'api',
pathForType(modelName) {
let dasherized = Ember.String.dasherize(modelName);
if (/^job-/.test(modelName)) {
dasherized = 'job';
}
return Ember.String.pluralize(dasherized);
},
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle',
actions: {
createGoodJob() {
let store = this.get('store');
let job = store.createRecord('job-good');
job.save().then((job) => {
alert('good job!');
console.log('good job created', job);
});
},
createBadJob() {
let store = this.get('store');
let job = store.createRecord('job-bad');
job.save().then((job) => {
alert('BAD job!');
console.log('bad job created', job);
});
}
}
});
export default Ember.Route.extend({
init() {
this._super(...arguments);
$.mockjax({
url: '/api/mock',
responseText: {
text: "someText"
}
});
$.mockjax({
url: '/api/jobs',
type: 'post',
responseText: {
"job": {
"type": "copy-class",
"id": "3bda5a99235f3ae887bc5d1e17081a8e90c2f6dd",
"status": "I"
}
}
});
},
model() {
return $.getJSON('/api/mock');
}
});
import DS from 'ember-data';
export default DS.RESTSerializer.extend({
payloadKeyFromModelName(modelName) {
console.log('payloadKeyFromModelName', modelName, this._isJobSubModel(modelName));
if (this._isJobSubModel(modelName)) {
return 'job';
}
return this._super(...arguments);
},
_isJobSubModel(modelName) {
return /^job-/.test(modelName);
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
<button {{action 'createGoodJob'}}>Creat Good Job</button>
<button {{action 'createBadJob'}}>Creat Bad Job</button>
{{outlet}}
<br>
<br>
import DS from 'ember-data';
export default DS.Model.extend({
isDeadEnd() {
return true;
}
});
import DS from 'ember-data';
export default DS.Model.extend({
hasBenefits() {
return true;
}
});
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string')
});
{
"version": "0.4.16",
"EmberENV": {
"FEATURES": {}
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "1.13.10",
"ember-data": "1.13.14",
"ember-template-compiler": "1.13.10",
"jquery-mockjax": "https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.min.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.