Skip to content

Instantly share code, notes, and snippets.

@barelyknown
Created November 20, 2015 22:32
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 barelyknown/22be5b88d7515941c5f6 to your computer and use it in GitHub Desktop.
Save barelyknown/22be5b88d7515941c5f6 to your computer and use it in GitHub Desktop.
app/services/states.js
import Ember from 'ember';
export default Ember.Service.extend(Ember.PromiseProxyMixin, {
store: Ember.inject.service(),
query(limit, offset) {
return this.get('store').query('state', {
include: 'country,region',
page: {
limit: limit,
offset: offset
}
});
},
load: Ember.on('init', function() {
var loadedCount = 0;
const limit = 100;
var offset = 0;
const queries = [];
this.set('promise', new Promise((resolve) => {
this.query(limit, offset).then((states) => {
loadedCount += states.get('length');
const totalRemaining = states.get('meta.record-count') - loadedCount;
const totalPagesRemaining = totalRemaining / limit;
var n = 0;
const promises = [];
while (n < totalPagesRemaining) {
n += 1;
promises.pushObject(this.query(limit, limit * n));
}
Ember.RSVP.all(promises).then(()=> {
resolve(this.get('store').peekAll('state'));
});
});
}));
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment