/static/js/models/socialprovidercollection.js
JavaScript | 99 lines | 87 code | 10 blank | 2 comment | 7 complexity | 12e8c3aaada298badd50f7d78e9cdb1f MD5 | raw file
Possible License(s): Apache-2.0
- define([
- 'config',
- 'underscore',
- 'backbone',
- 'lib/service',
- 'models/socialprovidermodel',
- 'logger',
- 'lib/assert',
- 'lib/errors'
- ], function(
- config,
- util,
- Backbone,
- Service,
- SocialProviderModel,
- logger,
- assert,
- errors
- ){
- // Wrap an optional error callback with a fallback error event.
- var wrapError = function(onError, originalModel, options) {
- return function(model, resp) {
- resp = model === originalModel ? resp : model;
- if (onError) {
- onError(model, resp, options);
- } else {
- originalModel.trigger('error', model, resp, options);
- }
- };
- };
- var SocialCollection = Backbone.Collection.extend({
- url: '/social_api/status',
- initialize: function(){
- },
- model: SocialProviderModel,
- fetch : function(options) {
- options = options||{};
- var collection = this;
- var success = options.success;
- options.success = function(resp, status, xhr) {
- collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options);
- if (success) success(collection, resp);
- };
- options.error = function(xhr, status){
- var resp;
- try {
- resp = xhr.responseText ? JSON.parse(xhr.responseText) : xhr;
- }
- catch (e) {
- resp = {};
- }
- if (resp.success) {
- options.success(resp);
- } else {
- return wrapError(onerror, collection, options)(collection, resp);
- }
- };
- return (this.sync || Backbone.sync).call(this, 'read', this, options);
- },
- parse: function(resp){
- var services = [],
- serviceData = SocialCollection.serviceData;
- util.each(resp.services, function(props, name){
- props = util.extend(props, serviceData[name] || {});
- props.name = name;
- props.title = props.connected ? props.connectedTitle : props.title;
- services.push(props);
- });
- return services;
- }
- });
- SocialCollection.serviceData = {};
-
- // TODO: externalize and localize this?
- SocialCollection.serviceData.twitter = {
- 'title': 'Connect to Twitter',
- 'connectedTitle': 'Twitter',
- 'intro': 'Authorize pancake to see your twitter data',
- 'requestServiceName': 'twitter',
- 'requestServiceMethod': 'oauth'
- };
- SocialCollection.serviceData.facebook = {
- 'title': 'Connect to Facebook',
- 'connectedTitle': 'Facebook',
- 'intro': 'Authorize pancake to see your facebook data',
- 'requestServiceName': 'facebook',
- 'requestServiceMethod': 'oauth'
- };
- return SocialCollection;
- });