/static/js/models/socialmodel.js
JavaScript | 155 lines | 104 code | 24 blank | 27 comment | 3 complexity | 7308e631ca82ccd9ea62d8b9bd022e24 MD5 | raw file
Possible License(s): Apache-2.0
- define([
- 'config',
- 'underscore',
- 'backbone',
- 'models/livecollection',
- 'models/postcollection',
- 'models/basemodel',
- 'lib/template',
- 'lib/urlhelper',
- 'lib/objecttools',
- 'logger',
- 'lib/errors',
- 'lib/lazily'
- ], function (
- config,
- util,
- Backbone,
- LiveCollection,
- PostCollection,
- BaseModel,
- template,
- URLHelper,
- objectTools,
- logger,
- errors,
- lazily
- ) {
- var plainObject = {};
- var flagError = function(msg){
- return function(err) {
- var args = Array.prototype.slice.call(arguments);
- args.unshift(msg);
- logger.error.apply(logger, args);
- };
- };
- var __BaseModel = BaseModel.prototype;
- // A model that handles the `/social_api/all` 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 PostCollection and a Backbone.Collection instances that
- // can be used with existing views.
- var SocialModel = BaseModel.extend({
- initialize: function (attributes, options) {
- options = options || {};
-
- this.serviceDefaults = options.services || {};
- var helper = new URLHelper({
- tokens: {
- base: '/',
- path: 'social_api/all'
- }
- });
- helper.update(options.tokens, options.query);
- this.url = util.bind(helper.url, helper);
-
- this.bind('success', this.onSuccess, this);
- return this;
- },
- posts: lazily(function () {
- var postCollection = new PostCollection([], {
- urlTokens: { path: 'social' }
- });
- // Customize response key translator used by postCollection.
- postCollection.translateKeys = util.bind(objectTools.translate, null, {
- 'title': 'place_title',
- 'uri': 'place_url',
- 'url': 'place_url',
- 'friend_img': 'avatar',
- // Results may vary re: author vs friend_name. We're passing back `author`
- // for news via results.
- 'author': 'author',
- 'friend_name': 'author',
- // The social API returns time as a delta under this key.
- 'time_delta': 'published',
- // The search API returns time as an absolute value under this key.
- 'time': 'published',
- 'service_img': 'icon',
- 'summary': 'summary',
- 'identifier': 'id'
-
- // TODO: this property does not exist on the API yet.
- // 'service': 'service',
- // TODO: this property does not exist on the API yet.
- // 'authorUrl': 'authorUrl',
- });
- return postCollection;
- }),
- services: lazily(function () {
- var servicesCollection = new LiveCollection();
- var serviceDefaults = this.serviceDefaults;
- // Create a custom parse method that yoinks the data we're interested in.
- servicesCollection.parse = function (resp) {
- var services = util.map(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 = util.defaults({
- 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);
- return model;
- });
- return services;
- };
- return servicesCollection;
- }),
- // Whenever a fetch comes back a success, take care of these additional
- // jobs.
- onSuccess: function (resp, options) {
- this.posts().process(resp, options);
- this.services().process(resp, util.extend({ add: false }, options));
- },
- parse: function (resp) {
- // Throw away response -- we store details in the appendage collections,
- // not in the model
- return plainObject;
- },
- reset: function(options){
- this.posts().reset([], options);
- this.services().reset([], options);
- }
- });
-
- return SocialModel;
- });