PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/models/sitecollection.js

https://bitbucket.org/yashdoshi89/rjs-compile-test
JavaScript | 197 lines | 114 code | 25 blank | 58 comment | 11 complexity | f575991825566c5a1b895d317cde355e MD5 | raw file
  1. define([
  2. 'config!',
  3. 'underscore',
  4. 'models/livecollection',
  5. 'models/sitemodel',
  6. 'models/thumbnailjobcollection',
  7. 'lib/translatekeys',
  8. 'lib/template',
  9. 'lib/urlhelper'
  10. ], function(
  11. config, util, LiveCollection,
  12. SiteModel, ThumbnailJobCollection,
  13. translateKeys,
  14. template,
  15. URLHelper
  16. ){
  17. var SiteCollection = LiveCollection.extend({
  18. model: SiteModel,
  19. resultAttributeMap: {
  20. // we want attributes.primaryType to be populated with the value of attributes.type
  21. 'primaryType': 'type',
  22. 'url': 'uri',
  23. 'thumbnailKey': 'thumbnail_key',
  24. 'thumbnailStatus': 'thumbnail_status'
  25. },
  26. initialize: function (models, options) {
  27. // Mix in some default options.
  28. options = options || {};
  29. // Create a new URL helper with defaults.
  30. var helper = new URLHelper({
  31. // Set a default template for URLHelper
  32. template: '{ base }{ path }{ query }',
  33. // Configure some default token values for URLHelper
  34. tokens: {
  35. base: config.apiRoot,
  36. path: 'top'
  37. }
  38. });
  39. // Update helper with any config passed in through options.
  40. helper.update(options.tokens, options.query);
  41. // Point this.url to URLHelper's url method.
  42. this.url = util.bind(helper.url, helper);
  43. // keep optional mapping of item data property keys to model properties
  44. if(options.attributeMap){
  45. this.attributeMap = util.defaults(this.attributeMap || {}, options.attributeMap);
  46. }
  47. // Any time a new site collection is fetched, start a new
  48. // thumbnail job collection for it.
  49. this.bind('reset', this.startThumbnailJob, this);
  50. this.bind('add', this.startThumbnailJob, this);
  51. },
  52. startThumbnailJob: function () {
  53. var thumbnailJob = this.thumbnailJob;
  54. if (
  55. // Don't start a job if the ID comes back null
  56. !this.thumbnailJobID ||
  57. (
  58. // Don't start the same job twice.
  59. thumbnailJob &&
  60. thumbnailJob.jobID === this.thumbnailJobID
  61. )
  62. ) return;
  63. // Create a new thumbnail job collection. Set the ID of the
  64. // thumbnail job (which ThumbnailJobCollection turns into a URL).
  65. var thumbnailJobCollection = new ThumbnailJobCollection({}, {
  66. jobID: this.thumbnailJobID
  67. });
  68. // When the thumbnails job comes back populated, update siteModels
  69. // in this collection.
  70. thumbnailJobCollection.bind(
  71. 'reset',
  72. this.updateModelsWithThumbnails,
  73. this
  74. );
  75. // Go get 'em tiger.
  76. thumbnailJobCollection.pollUntilFinishedProcessing();
  77. this.thumbnailJob = thumbnailJobCollection;
  78. },
  79. updateModelsWithThumbnails: function (thumbnailJobCollection) {
  80. var siteCollection = this;
  81. siteCollection.each(function (site) {
  82. // Get the site's thumbnail key...
  83. var thumbKey = site.get('thumbnailKey'),
  84. siteThumbStatus = site.get('thumbnailStatus'),
  85. siteThumbUrl = site.get('thumbnailUrl'),
  86. // Get the thumbnail record by its ID (same as thumbnail key)
  87. thumbModel = thumbnailJobCollection.get(thumbKey),
  88. thumbStatus = thumbModel ? thumbModel.get('status') : null,
  89. thumbUrl = thumbModel ? thumbModel.src() : null,
  90. update = {};
  91. // If status has changed or is new...
  92. if (siteThumbStatus !== thumbStatus)
  93. update.thumbnailStatus = thumbStatus;
  94. // If the URL is changed or new...
  95. if (siteThumbUrl !== thumbUrl)
  96. update.thumbnailUrl = thumbUrl;
  97. // Update the site with changes. Will fire an event
  98. // on the site record.
  99. if (!util.isEmpty(update)) site.set(update);
  100. });
  101. },
  102. // Get the value of an property in an object, optionally using a dot-path (some.deep.object)
  103. _getObject: function(name, obj){
  104. // TODO: could be a util/lang helper - not really class or collection-specific
  105. var parts = name.split('.'),
  106. pname = null;
  107. while((pname = parts.shift())){
  108. obj = obj[pname];
  109. }
  110. return obj;
  111. },
  112. // _setValue: function(name, value, obj) {
  113. //
  114. // },
  115. _applyAttributeMap: function(data, attributeMap){
  116. // translate data
  117. // could be a util/lang helper - not really class or collection-specific
  118. var mapping = attributeMap || this.attributeMap,
  119. getObject = this._getObject;
  120. // { 'alias': 'source' }
  121. util.each(mapping, function(name, alias, map){
  122. if(name in data) {
  123. data[alias] = name.indexOf(".") > -1 ?
  124. getObject(name, data) :
  125. data[name]
  126. ;
  127. // could *replace* by deleting the original key, but we should deep-clone the data at that point
  128. }
  129. });
  130. return data;
  131. },
  132. // Backbone.Collection's default parse method passes sync (xhr) results
  133. // directly to Collection.set. We're defining a custom parse
  134. // implementation that massages data from our JSON API into
  135. // a Backbone.Model-compatible format.
  136. //
  137. // Sample return data:
  138. // {
  139. // "results": [
  140. // {
  141. // "sortindex": 10,
  142. // "title": "Join.me",
  143. // "uri": "https://join.me/",
  144. // "thumbnail_key": "...",
  145. // "thumbnail_status": "processing",
  146. // "type": "suggestion",
  147. // "id": "...",
  148. // "types": ["suggestion"]
  149. // },
  150. // ...
  151. // ],
  152. // "success": true,
  153. // "thumbnails_job": "..."
  154. // }
  155. parse: function (resp, xhr) {
  156. // If the API considers this a success (different from HTTP response)...
  157. this.isSuccess = resp.success;
  158. this.thumbnailJobID = resp.thumbnails_job;
  159. // If API success, translate results to new format and
  160. // pass back. Otherwise, pass empty array.
  161. if(resp.success) {
  162. resp = util.map(resp.results, function(result) {
  163. return this._applyAttributeMap(result, this.resultAttributeMap);
  164. }, this);
  165. } else {
  166. resp = [];
  167. }
  168. return resp;
  169. }
  170. });
  171. return SiteCollection;
  172. });