Skip to content

Instantly share code, notes, and snippets.

@Gowiem
Last active June 6, 2017 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gowiem/4d29e1e823862b31c3c3a8d6a90cf91f to your computer and use it in GitHub Desktop.
Save Gowiem/4d29e1e823862b31c3c3a8d6a90cf91f to your computer and use it in GitHub Desktop.
Useful snippets to abstract out EmberIgniter's Rails JSONAPI + Pagination Example
import Ember from 'ember';
export default Ember.Mixin.create({
normalizeQueryResponse(store, clazz, payload) {
const result = this._super(...arguments);
result.meta = result.meta || {};
if (payload.links) {
result.meta.pagination = this.createPageMeta(payload.links);
}
return result;
},
createPageMeta(data) {
let meta = {};
Object.keys(data).forEach(type => {
const link = data[type];
meta[type] = {};
let anchor = document.createElement('a');
anchor.href = link;
anchor.search.slice(1).split('&').forEach(pairs => {
const [param, value] = pairs.split('=');
if (param == 'page%5Bnumber%5D') {
meta[type].number = parseInt(value);
}
if (param == 'page%5Bsize%5D') {
meta[type].size = parseInt(value);
}
});
anchor = null;
});
return meta;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment