UNPKG

wejsv2old-plugin-cdp-profile

Version:
200 lines (184 loc) 5.94 kB
$( document ).ready(function (){ // Map user routers App.Router.map(function() { this.resource('users',{path: '/user'}, function(){ this.resource('user',{ path: '/:user_id' }, function(){ this.route('relatos'); this.route('comunidades'); this.route('amigos'); this.route('cursos'); this.route('selos'); }); }); }); // route /user/:uid/ App.UserRoute = Ember.Route.extend({ queryParams: { edit: {} }, model: function(params) { var store = this.store; // get the user var hash = { user: this.store.find('user', params['user_id']), counts: { relatos: {}, comunidades: 0, cursos: 0 }, ownProfile: false, cursos: [], comunidades: [] }; // get contact relation if( App.currentUser.id === params['user_id'] ){ hash.contact = { status: 'currentUser' }; hash.ownProfile = true; } else if(App.currentUser.id) { hash.contact = new Ember.RSVP.Promise(function(resolve) { Ember.$.ajax({ type: 'GET', url: '/api/v1/user/'+params['user_id']+'/contact', dataType: 'json', contentType: 'application/json' }).done(function(data){ if(data.contact){ resolve( store.push('contact', data.contact) ); }else{ resolve( Ember.Object.create({status: ''}) ); } }).fail(function(){ resolve({}); }); }); }else{ hash.contact = {}; } hash.relatos = new Ember.RSVP.Promise( function(resolve) { var urlRequest = '/api/v1/relatos-from-user/' + params['user_id']; if ( hash.ownProfile ) urlRequest += '?owner=true'; Ember.$.ajax({ type: 'GET', url: urlRequest, dataType: 'json', contentType: 'application/json' }).done( function (data){ if( data && data.relato ){ hash.counts.relatos = data.meta.count || { creator: 0, autor: 0, ator: 0, }; resolve( store.pushMany('relato', data.relato) ); }else{ resolve([]); } }).fail(function(){ resolve([]); }); }); return Ember.RSVP.hash(hash); }, afterModel: function (model, transition){ return new Ember.RSVP.Promise( function(resolve) { $.getJSON('/api/v1/get-drupal-user-id/' + model.user.id) .then(function (data){ if ( data && data.drupalId ) { model.user.set('drupalId', data.drupalId); return $.getJSON(we.configs.client.publicVars.links.timeline + '/user-courses/' + model.user.get('drupalId')); } resolve(); }) .then(function (cursos){ model.counts.cursos = cursos.length; model.cursos = cursos.map(function (curso, i) { if ( i === 0 ) { curso.active = true; } curso.link = we.configs.client.publicVars.links.timeline + '/courses/' + curso.id; return curso; }); return $.getJSON(we.configs.client.publicVars.links.timeline + '/user-communities/' + model.user.get('drupalId')); }) .done(function (comunidades){ model.counts.comunidades = comunidades.length; model.comunidades = comunidades.map(function (comunidade, i){ if ( i === 0 ) { comunidade.active = true; } comunidade.link = we.configs.client.publicVars.links.timeline + '/node/' + comunidade.id; return comunidade; }); resolve(); }) .fail(function (data){ Ember.Logger.error('Error on get user drupalId and Drupal Data', data); resolve(); }); }); } }); // route /user/:uid/index App.UserIndexRoute = Ember.Route.extend(App.ResetScrollMixin,{ beforeModel: function (){ var interestVocabulary = this.get('WeConfigs.client.publicVars.vocabularies.interests'); return this.store.find('term', { vocabulary: interestVocabulary, populate: false, limit: 1000 }); }, model: function() { return Ember.RSVP.hash({ user: this.modelFor('user').user, relatos: this.modelFor('user').relatos.filter(function (relato, i, arr){ return i <= 2; }), cursos: this.modelFor('user').cursos, comunidades: this.modelFor('user').comunidades, ownProfile: this.modelFor('user').ownProfile }); } }); App.UserRelatosRoute = Ember.Route.extend(App.ResetScrollMixin,{ queryParams: { type: { refreshModel: true }, page: { refreshModel: true } }, resetController: function (controller, isExiting, transition) { if (isExiting) { // isExiting would be false if only the route's model was changing controller.set('page', 1); controller.set('type', null); } }, model: function() { return Ember.RSVP.hash({ relatos: this.modelFor('user').relatos, counts: this.modelFor('user').counts }); } }); App.UserCursosRoute = Ember.Route.extend(App.ResetScrollMixin,{ model: function() { return Ember.RSVP.hash({ user: this.modelFor('user').user, cursos: this.modelFor('user').cursos, ownProfile: this.modelFor('user').ownProfile }); } }); App.UserComunidadesRoute = Ember.Route.extend(App.ResetScrollMixin,{ model: function() { return Ember.RSVP.hash({ user: this.modelFor('user').user, comunidades: this.modelFor('user').comunidades, ownProfile: this.modelFor('user').ownProfile }); } }); });