PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/mozillapancake/pancake
JavaScript | 161 lines | 106 code | 25 blank | 30 comment | 3 complexity | c3f3064688f45f829ec548016d6ed715 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. 'backbone',
  8. 'models/livecollection',
  9. 'models/postcollection',
  10. 'models/basemodel',
  11. 'lib/template',
  12. 'lib/urlhelper',
  13. 'lib/objecttools',
  14. 'logger',
  15. 'lib/errors',
  16. 'lib/lazily'
  17. ], function (
  18. config,
  19. util,
  20. Backbone,
  21. LiveCollection,
  22. PostCollection,
  23. BaseModel,
  24. template,
  25. URLHelper,
  26. objectTools,
  27. logger,
  28. errors,
  29. lazily
  30. ) {
  31. var plainObject = {};
  32. var flagError = function(msg){
  33. return function(err) {
  34. var args = Array.prototype.slice.call(arguments);
  35. args.unshift(msg);
  36. logger.error.apply(logger, args);
  37. };
  38. };
  39. var __BaseModel = BaseModel.prototype;
  40. // A model that handles the `/social_api/all` endpoint.
  41. // This endpoint contains information for:
  42. //
  43. // * Social site login status
  44. // * Sites shared via social networks
  45. // * Thumbnail Job for sites.
  46. //
  47. // This model handles hitting the endpoint and turning the responses
  48. // into separate PostCollection and a Backbone.Collection instances that
  49. // can be used with existing views.
  50. var SocialModel = BaseModel.extend({
  51. initialize: function (attributes, options) {
  52. options = options || {};
  53. this.serviceDefaults = options.services || {};
  54. var helper = new URLHelper({
  55. tokens: {
  56. base: '/',
  57. path: 'social_api/all'
  58. }
  59. });
  60. helper.update(options.tokens, options.query);
  61. this.url = util.bind(helper.url, helper);
  62. this.bind('success', this.onSuccess, this);
  63. return this;
  64. },
  65. posts: BaseModel.subcollection('posts', function () {
  66. var postCollection = new PostCollection([], {
  67. urlTokens: { path: 'social' }
  68. });
  69. // Customize response key translator used by postCollection.
  70. postCollection.translateKeys = util.bind(objectTools.translate, null, {
  71. 'title': 'place_title',
  72. 'uri': 'place_url',
  73. 'url': 'place_url',
  74. 'friend_img': 'avatar',
  75. // Results may vary re: author vs friend_name. We're passing back `author`
  76. // for news via results.
  77. 'author': 'author',
  78. 'friend_name': 'author',
  79. 'friend_username': 'username',
  80. // The social API returns time as a delta under this key.
  81. 'time_delta': 'published',
  82. // The search API returns time as an absolute value under this key.
  83. 'time': 'published',
  84. 'service_img': 'icon',
  85. 'service': 'service',
  86. 'summary': 'summary',
  87. 'identifier': 'id'
  88. // TODO: this property does not exist on the API yet.
  89. // 'service': 'service',
  90. // TODO: this property does not exist on the API yet.
  91. // 'authorUrl': 'authorUrl',
  92. });
  93. return postCollection;
  94. }),
  95. services: lazily(function () {
  96. var servicesCollection = new LiveCollection();
  97. var serviceDefaults = this.serviceDefaults;
  98. // Create a custom parse method that yoinks the data we're interested in.
  99. servicesCollection.parse = function (resp) {
  100. var services = util.map(resp.services, function (service) {
  101. var serviceOptions = {
  102. stateData: serviceDefaults[service.name] || {}
  103. };
  104. var state = service.login ? 'disconnected' : 'connected';
  105. // ..provide the hash of properties corresponding to their logged-in/out state in the options.
  106. var model = util.defaults({
  107. name: service.name,
  108. // stash the login state in 'initialState' attribute
  109. // we want to set this when the views etc. are ready to receive the event
  110. initialState: state,
  111. state: state
  112. }, serviceOptions);
  113. return model;
  114. });
  115. return services;
  116. };
  117. return servicesCollection;
  118. }),
  119. // Whenever a fetch comes back a success, take care of these additional
  120. // jobs.
  121. onSuccess: function (resp, options) {
  122. this.posts().process(resp, options);
  123. this.services().process(resp, util.extend({ add: false }, options));
  124. },
  125. parse: function (resp) {
  126. // Throw away response -- we store details in the appendage collections,
  127. // not in the model
  128. return plainObject;
  129. },
  130. clear: function(options){
  131. this.posts().reset([], options);
  132. this.services().reset([], options);
  133. }
  134. });
  135. return SocialModel;
  136. });