Last active
August 29, 2015 14:24
-
-
Save jwo/2bc5b7752a6b45df0109 to your computer and use it in GitHub Desktop.
Ember Data 1.13 serializer; enable when server gives data in slightly wrong way
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
// Allows json to not have a root element | |
// Allows embedded records for "images" instead of "image_ids" or "images" being the ids | |
import DS from 'ember-data'; | |
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, { | |
isNewSerializerAPI: true, | |
attrs: { | |
images: {embedded: 'always'}, | |
}, | |
normalizeResponse: function(store, primaryModelClass, payload, id, requestType, isSingle) { | |
return this._super(store, primaryModelClass, {gallery: payload}, id, requestType, isSingle); | |
} | |
}); |
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
// Fetches all galleries from "/home" instead of "/galleries" | |
// Will fetch a singular gallery from "/gallery/:id" instead of "galleries" | |
import Ember from 'ember'; | |
import ApplicationAdapter from './application'; | |
export default ApplicationAdapter.extend({ | |
urlForFindAll: function(){ | |
return this._super("home"); | |
}, | |
pathForType: function(modelName) { | |
var decamelized = Ember.String.decamelize(modelName); | |
if (decamelized === "gallery"){ | |
return "gallery"; | |
} else if (decamelized === "home"){ | |
return "home"; | |
} else { | |
return Ember.String.pluralize(decamelized); | |
} | |
} | |
}); |
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
[ | |
{ | |
"id": "765bbbc3e13d", | |
"created_at": 1435766060, | |
"updated_at": 1436800257, | |
"likes": 52, | |
"images": [ | |
{ | |
"id": "a39658ec9001", | |
"url": "https://media0.giphy.com/media/nhmmGlocGCJ3y/200.gif", | |
"created_at": 1435766060, | |
"updated_at": 1435766060 | |
}, | |
{ | |
"id": "bb81fbbb9f21", | |
"url": "https://media0.giphy.com/media/nhmmGlocGCJ3y/200.gif", | |
"created_at": 1435766060, | |
"updated_at": 1435766060 | |
}, | |
{ | |
"id": "d9afd2abe285", | |
"url": "https://media0.giphy.com/media/nhmmGlocGCJ3y/200.gif", | |
"created_at": 1435766060, | |
"updated_at": 1435766060 | |
}, | |
{ | |
"id": "be4cb9800288", | |
"url": "https://media0.giphy.com/media/nhmmGlocGCJ3y/200.gif", | |
"created_at": 1435766060, | |
"updated_at": 1435766060 | |
}, | |
{ | |
"id": "58ded825f454", | |
"url": "https://media0.giphy.com/media/nhmmGlocGCJ3y/200.gif", | |
"created_at": 1435766060, | |
"updated_at": 1435766060 | |
}, | |
{ | |
"id": "c165de2a5594", | |
"url": "https://media0.giphy.com/media/nhmmGlocGCJ3y/200.gif", | |
"created_at": 1435766060, | |
"updated_at": 1435766060 | |
} | |
], | |
}, | |
{ | |
"id": "a55315ada32d", | |
"created_at": 1435765984, | |
"updated_at": 1435765984, | |
"likes": 0, | |
"images": [ | |
{ | |
"id": "53a8c2b3a544", | |
"url": "https://media1.giphy.com/media/Mqf3rcvP1hBTO/200.gif", | |
"created_at": 1435765984, | |
"updated_at": 1435765984 | |
}, | |
{ | |
"id": "0070b4fdedc9", | |
"url": "https://media1.giphy.com/media/Mqf3rcvP1hBTO/200.gif", | |
"created_at": 1435765984, | |
"updated_at": 1435765984 | |
} | |
], | |
}, | |
{ | |
"id": "9a27f2651100", | |
"created_at": 1435678755, | |
"updated_at": 1435678755, | |
"likes": 0, | |
"images": [ | |
{ | |
"id": "d97da45f817f", | |
"url": "http://i.imgur.com/59FG2S9.jpg", | |
"created_at": 1435678755, | |
"updated_at": 1435683694 | |
} | |
] | |
}, | |
{ | |
"id": "741615b49c00", | |
"created_at": 1435678755, | |
"updated_at": 1435678755, | |
"likes": 0, | |
"images": [ | |
{ | |
"id": "5d1d9e18690f", | |
"url": "http://i.imgur.com/prolD1E.jpg", | |
"created_at": 1435678755, | |
"updated_at": 1435683694 | |
} | |
], | |
}, | |
{ | |
"id": "9afd0734876f", | |
"created_at": 1435678755, | |
"updated_at": 1435772438, | |
"likes": 20, | |
"images": [ | |
{ | |
"id": "b34f5c8f61f2", | |
"url": "http://i.imgur.com/PEUuhKK.jpg", | |
"created_at": 1435678755, | |
"updated_at": 1435683694 | |
}, | |
{ | |
"id": "88381388ea15", | |
"url": "http://i.imgur.com/snBZr7N.gif", | |
"created_at": 1435678755, | |
"updated_at": 1435683694 | |
} | |
] | |
} | |
] |
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({ | |
likes: DS.attr('string'), | |
thumbnail: DS.attr('string'), | |
images: DS.hasMany('image', {async: true} ) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment