PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/mozillapancake/pancake
JavaScript | 103 lines | 87 code | 11 blank | 5 comment | 7 complexity | 7883b6becc60c6e21c4fadc37807f3f5 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. 'lib/service',
  9. 'models/socialprovidermodel',
  10. 'logger',
  11. 'lib/assert',
  12. 'lib/errors'
  13. ], function(
  14. config,
  15. util,
  16. Backbone,
  17. Service,
  18. SocialProviderModel,
  19. logger,
  20. assert,
  21. errors
  22. ){
  23. // Wrap an optional error callback with a fallback error event.
  24. var wrapError = function(onError, originalModel, options) {
  25. return function(model, resp) {
  26. resp = model === originalModel ? resp : model;
  27. if (onError) {
  28. onError(model, resp, options);
  29. } else {
  30. originalModel.trigger('error', model, resp, options);
  31. }
  32. };
  33. };
  34. var SocialCollection = Backbone.Collection.extend({
  35. url: '/social_api/status',
  36. initialize: function(){
  37. },
  38. model: SocialProviderModel,
  39. fetch : function(options) {
  40. options = options||{};
  41. var collection = this;
  42. var success = options.success;
  43. options.success = function(resp, status, xhr) {
  44. collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options);
  45. if (success) success(collection, resp);
  46. };
  47. options.error = function(xhr, status){
  48. var resp;
  49. try {
  50. resp = xhr.responseText ? JSON.parse(xhr.responseText) : xhr;
  51. }
  52. catch (e) {
  53. resp = {};
  54. }
  55. if (resp.success) {
  56. options.success(resp);
  57. } else {
  58. return wrapError(onerror, collection, options)(collection, resp);
  59. }
  60. };
  61. return (this.sync || Backbone.sync).call(this, 'read', this, options);
  62. },
  63. parse: function(resp){
  64. var services = [],
  65. serviceData = SocialCollection.serviceData;
  66. util.each(resp.services, function(props, name){
  67. props = util.extend(props, serviceData[name] || {});
  68. props.name = name;
  69. props.title = props.connected ? props.connectedTitle : props.title;
  70. services.push(props);
  71. });
  72. return services;
  73. }
  74. });
  75. SocialCollection.serviceData = {};
  76. // TODO: externalize and localize this?
  77. SocialCollection.serviceData.twitter = {
  78. 'title': 'Connect to Twitter',
  79. 'connectedTitle': 'Twitter',
  80. 'intro': 'Authorize pancake to see your twitter data',
  81. 'requestServiceName': 'twitter',
  82. 'requestServiceMethod': 'oauth'
  83. };
  84. SocialCollection.serviceData.facebook = {
  85. 'title': 'Connect to Facebook',
  86. 'connectedTitle': 'Facebook',
  87. 'intro': 'Authorize pancake to see your facebook data',
  88. 'requestServiceName': 'facebook',
  89. 'requestServiceMethod': 'oauth'
  90. };
  91. return SocialCollection;
  92. });