-
-
Save bmx269/39d800f71d04b03fe57c0e63543405eb to your computer and use it in GitHub Desktop.
Ember adapter and serializer for Drupal 8 with JSON:API
This file contains hidden or 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
// app/app.js | |
import Ember from 'ember'; | |
import Resolver from './resolver'; | |
import loadInitializers from 'ember-load-initializers'; | |
import config from './config/environment'; | |
import './models/custom-inflector-rules'; // <-Add this line for the rules. |
This file contains hidden or 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
// app/adapters/application.js | |
import DS from 'ember-data'; | |
export default DS.JSONAPIAdapter.extend({ | |
host: 'http://0.0.0.0:8888', | |
namespace: 'jsonapi', | |
buildURL(record, suffix) { | |
return this._super(record, suffix) + '?_format=api_json'; | |
}, | |
pathForType(type) { | |
return type.replace('--', '/').replace('-', '_'); | |
} | |
}); |
This file contains hidden or 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
// app/serializers/application.js | |
import Ember from 'ember'; | |
import DS from 'ember-data'; | |
export default DS.JSONAPISerializer.extend({ | |
modelNameFromPayloadKey(key) { | |
return Ember.String.dasherize(key); | |
}, | |
keyForAttribute(attr) { | |
return Ember.String.underscore(attr); | |
}, | |
keyForRelationship(key) { | |
return Ember.String.underscore(key); | |
} | |
}); |
This file contains hidden or 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
// app/models/custom-inflector-rules.js | |
import Inflector from 'ember-inflector'; | |
const inflector = Inflector.inflector; | |
inflector.irregular('node--article', 'node--article'); | |
// Meet Ember Inspector's expectation of an export | |
export default {}; |
This file contains hidden or 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
// app/models/node--article.js | |
import DS from 'ember-data'; | |
export default DS.Model.extend({ | |
nid: DS.attr(), | |
uuid: DS.attr(), | |
title: DS.attr(), | |
created: DS.attr(), | |
body: DS.attr(), | |
fieldComments: DS.attr(), | |
uid: DS.belongsTo('user--user') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment