PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/models/thumbnailjobcollection.js

https://bitbucket.org/yashdoshi89/rjs-compile-test
JavaScript | 106 lines | 56 code | 11 blank | 39 comment | 6 complexity | 83d033b2cfb6db49718bf2d4cf91d538 MD5 | raw file
  1. define([
  2. 'config!',
  3. 'underscore',
  4. 'backbone',
  5. 'models/livecollection',
  6. 'lib/template',
  7. 'lib/translatekeys'
  8. ], function (config, util, Backbone, LiveCollection, template, translateKeys) {
  9. // Cache underscore templates for URL endpoints.
  10. var makeStatusUrl = template(config.thumbnailerStatus),
  11. makeThumbnailUrl = template(config.thumbnailUrl);
  12. var ThumbnailModel = Backbone.Model.extend({
  13. // Builds you a thumbnail URL
  14. src: function () {
  15. return makeThumbnailUrl({
  16. thumbnail_key: this.get('id')
  17. });
  18. }
  19. });
  20. var ThumbnailJobCollection = LiveCollection.extend({
  21. model: ThumbnailModel,
  22. initialize: function (attributes, options) {
  23. this.jobID = '';
  24. // Create a URL from jobID, if none set.
  25. if (options && options.jobID && !this.url) {
  26. this.jobID = options.jobID;
  27. this.url = makeStatusUrl({ jobId: this.jobID });
  28. }
  29. console.log('New Thumbnail Job Collection ' + this.jobID);
  30. },
  31. pollUntilFinishedProcessing: function () {
  32. this.stream({
  33. interval: 5000,
  34. tries: 5,
  35. success: function (collection, resp) {
  36. console.log('Polled Thumbnail Job API (' +
  37. collection.length + 'ct) ' +
  38. collection.jobID);
  39. // If it's an XHR success, but not an API success do nothing.
  40. // TODO: handle API errors.
  41. if (!collection.isSuccess) return;
  42. // Get all of the statuses from thumbnails
  43. var statuses = collection.pluck('status');
  44. // If all thumbnails are processed, stop polling.
  45. if (util.indexOf(statuses, 'processing') === -1)
  46. collection.unstream();
  47. }
  48. });
  49. },
  50. // Defining a custom parse implementation that massages data
  51. // from our JSON API into a Backbone.Model-compatible format.
  52. //
  53. // Sample return data:
  54. //
  55. // {
  56. // "job": "...",
  57. // "sites": [
  58. // {
  59. // "key": "...",
  60. // "status": "processing",
  61. // "url": "http://instapaper.com/"
  62. // }
  63. // ...
  64. // ],
  65. // "success": true
  66. // }
  67. //
  68. // ...which is turned into:
  69. //
  70. // [
  71. // {
  72. // "id": "...",
  73. // "status": "processing",
  74. // "url": "http://instapaper.com/"
  75. // }
  76. // ...
  77. // ]
  78. parse: function (resp) {
  79. var results = resp.sites,
  80. translator = util.bind(translateKeys, this, {
  81. // Convert key to id, so we can use all of the nice
  82. // collection/model getters.
  83. key: 'id',
  84. status: 'status',
  85. url: 'url'
  86. });
  87. // Make sure JobID is correct
  88. this.jobID = resp.job;
  89. // Set if this is an API success
  90. this.isSuccess = resp.success;
  91. return (resp && resp.success ? util.map(results, translator) : []);
  92. }
  93. });
  94. return ThumbnailJobCollection;
  95. });