PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/pancake-web/pancake/web/static/js/models/sitecollection.js

https://bitbucket.org/mozillapancake/pancake
JavaScript | 82 lines | 43 code | 9 blank | 30 comment | 3 complexity | 1c1307f7209d2598831fc181c64c72fa MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, MIT, Apache-2.0
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. define([
  5. 'config',
  6. 'underscore',
  7. 'models/livecollection',
  8. 'models/sitemodel',
  9. 'models/thumbnails',
  10. 'lib/template',
  11. 'lib/urlhelper'
  12. ], function(
  13. config,
  14. util,
  15. LiveCollection,
  16. SiteModel,
  17. thumbnails,
  18. template,
  19. URLHelper
  20. ){
  21. var SiteCollection = LiveCollection.extend({
  22. model: SiteModel,
  23. url: function (tokens, query) {
  24. return this.urlHelper.url(tokens, query);
  25. },
  26. initialize: function (models, options) {
  27. // Mix in some default options.
  28. options = options || {};
  29. if (!this.urlHelper) this.urlHelper = new URLHelper({
  30. template: '{ base }{ path }{ query }',
  31. tokens: {
  32. base: config.apiRoot,
  33. path: 'top'
  34. },
  35. query: { v: 1 }
  36. });
  37. this.urlHelper.update(options.urlTokens, options.urlQuery);
  38. },
  39. // Backbone.Collection's default parse method passes sync (xhr) results
  40. // directly to Collection.set. We're defining a custom parse
  41. // implementation that massages data from our JSON API into
  42. // a Backbone.Model-compatible format.
  43. //
  44. // Sample return data:
  45. //
  46. // {
  47. // "results": [
  48. // {
  49. // "sortindex": 10,
  50. // "title": "Join.me",
  51. // "uri": "https://join.me/",
  52. // "thumbnail_key": "...",
  53. // "thumbnail_status": "processing",
  54. // "type": "suggestion",
  55. // "id": "...",
  56. // "types": ["suggestion"]
  57. // },
  58. // ...
  59. // ],
  60. // "success": true,
  61. // "thumbnails_job": "..."
  62. // }
  63. parse: function (resp, xhr) {
  64. resp = thumbnails.parse.call(this, resp, xhr);
  65. var results = resp.d;
  66. // nothing to do here
  67. if(!results.length) return [];
  68. // Translate response array using curried translator function.
  69. return util.map(results, this.model.prototype.parse);
  70. }
  71. });
  72. return SiteCollection;
  73. });