/src/models/socialmodel.js
JavaScript | 83 lines | 50 code | 14 blank | 19 comment | 3 complexity | 95591ef7b1c1592ca3b7c21874df1b74 MD5 | raw file
- define([
- 'config!',
- 'underscore',
- 'backbone',
- 'models/sitecollection',
- 'lib/template',
- 'lib/urlhelper'
- ], function (config, util, Backbone, SiteCollection, template, URLHelper) {
- var SocialServiceModel = Backbone.Model;
- // A model that handles the `api/social` endpoint.
- // This endpoint contains information for:
- //
- // * Social site login status
- // * Sites shared via social networks
- // * Thumbnail Job for sites.
- //
- // This model handles hitting the endpoint and turning the responses
- // into separate SiteCollection and a Backbone.Collection instances that
- // can be used with existing views.
- var SocialModel = Backbone.Model.extend({
- initialize: function (attributes, options) {
- options = options || {};
-
- this.serviceDefaults = options.services || {};
-
- var helper = new URLHelper({
- tokens: {
- base: config.apiRoot,
- path: 'social'
- }
- });
-
- helper.update(options.tokens, options.query);
-
- this.url = util.bind(helper.url, helper);
-
- return this;
- },
-
- parse: function (resp) {
- // Create a collection for storing services information.
- var services = new Backbone.Collection(),
- // Create a SiteCollection for storing site info.
- sites = new SiteCollection([], {
- tokens: { path: 'social' }
- }),
- serviceDefaults = this.serviceDefaults;
-
- // Pass results to SiteCollection.parse(), adding
- // the result to the collection.
- // NB: results don't include ids. Should url stand in for id?
- sites.add(sites.parse(resp));
-
- // create models for each service
- util.each(resp.services, function (service) {
- var serviceOptions = {
- stateData: serviceDefaults[service.name] || {}
- };
-
- var state = service.login ? 'disconnected' : 'connected';
-
- // ..provide the hash of properties corresponding to their logged-in/out state in the options.
- var model = new SocialServiceModel({
- name: service.name,
- // stash the login state in 'initialState' attribute
- // we want to set this when the views etc. are ready to receive the event
- initialState: state,
- state: state
- }, serviceOptions );
-
- services.add(model);
- });
- return resp.success ? {
- services: services,
- sites: sites
- } : {};
- }
- });
-
- return SocialModel;
- });