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

/src/models/socialmodel.js

https://bitbucket.org/gordonbrander/rjs-compile-test
JavaScript | 83 lines | 50 code | 14 blank | 19 comment | 3 complexity | 95591ef7b1c1592ca3b7c21874df1b74 MD5 | raw file
  1. define([
  2. 'config!',
  3. 'underscore',
  4. 'backbone',
  5. 'models/sitecollection',
  6. 'lib/template',
  7. 'lib/urlhelper'
  8. ], function (config, util, Backbone, SiteCollection, template, URLHelper) {
  9. var SocialServiceModel = Backbone.Model;
  10. // A model that handles the `api/social` endpoint.
  11. // This endpoint contains information for:
  12. //
  13. // * Social site login status
  14. // * Sites shared via social networks
  15. // * Thumbnail Job for sites.
  16. //
  17. // This model handles hitting the endpoint and turning the responses
  18. // into separate SiteCollection and a Backbone.Collection instances that
  19. // can be used with existing views.
  20. var SocialModel = Backbone.Model.extend({
  21. initialize: function (attributes, options) {
  22. options = options || {};
  23. this.serviceDefaults = options.services || {};
  24. var helper = new URLHelper({
  25. tokens: {
  26. base: config.apiRoot,
  27. path: 'social'
  28. }
  29. });
  30. helper.update(options.tokens, options.query);
  31. this.url = util.bind(helper.url, helper);
  32. return this;
  33. },
  34. parse: function (resp) {
  35. // Create a collection for storing services information.
  36. var services = new Backbone.Collection(),
  37. // Create a SiteCollection for storing site info.
  38. sites = new SiteCollection([], {
  39. tokens: { path: 'social' }
  40. }),
  41. serviceDefaults = this.serviceDefaults;
  42. // Pass results to SiteCollection.parse(), adding
  43. // the result to the collection.
  44. // NB: results don't include ids. Should url stand in for id?
  45. sites.add(sites.parse(resp));
  46. // create models for each service
  47. util.each(resp.services, function (service) {
  48. var serviceOptions = {
  49. stateData: serviceDefaults[service.name] || {}
  50. };
  51. var state = service.login ? 'disconnected' : 'connected';
  52. // ..provide the hash of properties corresponding to their logged-in/out state in the options.
  53. var model = new SocialServiceModel({
  54. name: service.name,
  55. // stash the login state in 'initialState' attribute
  56. // we want to set this when the views etc. are ready to receive the event
  57. initialState: state,
  58. state: state
  59. }, serviceOptions );
  60. services.add(model);
  61. });
  62. return resp.success ? {
  63. services: services,
  64. sites: sites
  65. } : {};
  66. }
  67. });
  68. return SocialModel;
  69. });