docs.coffee 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. class app.collections.Docs extends app.Collection
  2. @model: 'Doc'
  3. sort: ->
  4. @models.sort (a, b) ->
  5. a = a.name.toLowerCase()
  6. b = b.name.toLowerCase()
  7. if a < b then -1 else if a > b then 1 else 0
  8. # Load models concurrently.
  9. # It's not pretty but I didn't want to import a promise library only for this.
  10. CONCURRENCY = 3
  11. load: (onComplete, onError, options) ->
  12. i = 0
  13. next = =>
  14. if i < @models.length
  15. @models[i].load(next, fail, options)
  16. else if i is @models.length + CONCURRENCY - 1
  17. onComplete()
  18. i++
  19. return
  20. fail = (args...) ->
  21. if onError
  22. onError(args...)
  23. onError = null
  24. next()
  25. return
  26. next() for [0...CONCURRENCY]
  27. return
  28. clearCache: ->
  29. doc.clearCache() for doc in @models
  30. return
  31. undownload: (callback) ->
  32. i = 0
  33. next = =>
  34. if i < @models.length
  35. @models[i++].undownload(next, next)
  36. else
  37. callback()
  38. next()
  39. getDownloadStatuses: (callback) ->
  40. app.db.versions @models, (statuses) ->
  41. if statuses
  42. for key, value of statuses
  43. statuses[key] = downloaded: !!value, version: value
  44. callback(statuses)