Created
November 20, 2015 22:32
-
-
Save barelyknown/22be5b88d7515941c5f6 to your computer and use it in GitHub Desktop.
app/services/states.js
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.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