Created
February 21, 2014 23:37
-
-
Save craigspaeth/9145979 to your computer and use it in GitHub Desktop.
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
_ = require 'underscore' | |
Backbone = require 'backbone' | |
redis = require 'redis' | |
module.exports = (artsyUrl) -> | |
# For paginated routes, this will recursively fetch until the end of the set. | |
# | |
# @param {Object} options Backbone sync options like `success` and `error` | |
fetchUntilEnd: (options = {}) -> | |
if window and data = redis.get('fetch-until-end:' + @url()) | |
@reset(data) | |
return options.success(@) | |
page = 0 | |
opts = _.clone(options) | |
fetchPage = => | |
@fetch _.extend opts, | |
data: _.extend (opts.data ? {}), page: page += 1 | |
remove: false | |
success: (col, res) => | |
redis.set('fetch-until-end:' + @url()) unless window | |
if res.length is 0 then options.success?(@) else fetchPage() | |
error: options.error | |
fetchPage() | |
# Fetches a set by key and populates the collection with the first result. | |
# | |
# @param {String} key | |
# @param {Object} options Backbone sync options like `success` and `error` | |
fetchSetItemsByKey: (key, options = {}) -> | |
new Backbone.Collection(null).fetch | |
url: "#{artsyUrl}/api/v1/sets?key=#{key}" | |
cache: options.cache | |
success: (sets) => | |
return options.success(@) unless sets.length | |
new Backbone.Collection(null).fetch | |
url: "#{artsyUrl}/api/v1/set/#{sets.first().get 'id'}/items" | |
cache: options.cache | |
success: (col) => | |
@reset col.toJSON() | |
options.success @ | |
error: options.error | |
error: options.error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment