Last active
December 25, 2019 18:18
-
-
Save elidupuis/17312c7d874019fa8b55 to your computer and use it in GitHub Desktop.
Extended Models with custom adapter and serializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DS from 'ember-data'; | |
export default DS.Model.extend({ | |
isDeadEnd() { | |
return true; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DS from 'ember-data'; | |
export default DS.Model.extend({ | |
hasBenefits() { | |
return true; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DS from 'ember-data'; | |
export default DS.Model.extend({ | |
title: DS.attr('string') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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