PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/static/scripts/mvc/user/user-model.js

https://bitbucket.org/chapmanb/galaxy-central/
JavaScript | 99 lines | 52 code | 14 blank | 33 comment | 5 complexity | d33f829e259e0253010a0643fe0c2f4b MD5 | raw file
  1. /** @class Model for a Galaxy user (including anonymous users).
  2. * @name User
  3. *
  4. * @augments Backbone.Model
  5. * @borrows LoggableMixin#logger as #logger
  6. * @borrows LoggableMixin#log as #log
  7. * @constructs
  8. */
  9. var User = Backbone.Model.extend( LoggableMixin ).extend(
  10. /** @lends User.prototype */{
  11. ///** logger used to record this.log messages, commonly set to console */
  12. //// comment this out to suppress log output
  13. //logger : console,
  14. /** API location for this resource */
  15. urlRoot : 'api/users',
  16. /** Model defaults
  17. * Note: don't check for anon-users with the username as the default is '(anonymous user)'
  18. * a safer method is if( !user.get( 'email' ) ) -> anon user
  19. */
  20. defaults : /** @lends User.prototype */{
  21. id : null,
  22. username : '(' + _l( "anonymous user" ) + ')',
  23. email : "",
  24. total_disk_usage : 0,
  25. nice_total_disk_usage : "",
  26. quota_percent : null
  27. },
  28. /** Set up and bind events
  29. * @param {Object} data Initial model data.
  30. */
  31. initialize : function( data ){
  32. this.log( 'User.initialize:', data );
  33. this.on( 'loaded', function( model, resp ){ this.log( this + ' has loaded:', model, resp ); });
  34. this.on( 'change', function( model, data ){ this.log( this + ' has changed:', model, data.changes ); });
  35. },
  36. isAnonymous : function(){
  37. return ( !this.get( 'email' ) );
  38. },
  39. /** Load a user with the API using an id.
  40. * If getting an anonymous user or no access to a user id, pass the User.CURRENT_ID_STR
  41. * (e.g. 'current') and the API will return the current transaction's user data.
  42. * @param {String} idOrCurrent encoded user id or the User.CURRENT_ID_STR
  43. * @param {Object} options hash to pass to Backbone.Model.fetch. Can contain success, error fns.
  44. * @fires loaded when the model has been loaded from the API, passing the newModel and AJAX response.
  45. */
  46. loadFromApi : function( idOrCurrent, options ){
  47. idOrCurrent = idOrCurrent || User.CURRENT_ID_STR;
  48. options = options || {};
  49. var model = this,
  50. userFn = options.success;
  51. /** @ignore */
  52. options.success = function( newModel, response ){
  53. model.trigger( 'loaded', newModel, response );
  54. if( userFn ){ userFn( newModel, response ); }
  55. };
  56. // requests for the current user must have a sep. constructed url (fetch don't work, ma)
  57. if( idOrCurrent === User.CURRENT_ID_STR ){
  58. options.url = this.urlRoot + '/' + User.CURRENT_ID_STR;
  59. }
  60. return BaseModel.prototype.fetch.call( this, options );
  61. },
  62. /** string representation */
  63. toString : function(){
  64. var userInfo = [ this.get( 'username' ) ];
  65. if( this.get( 'id' ) ){
  66. userInfo.unshift( this.get( 'id' ) );
  67. userInfo.push( this.get( 'email' ) );
  68. }
  69. return 'User(' + userInfo.join( ':' ) + ')';
  70. }
  71. });
  72. // string to send to tell server to return this transaction's user (see api/users.py)
  73. User.CURRENT_ID_STR = 'current';
  74. // class method to load the current user via the api and return that model
  75. User.getCurrentUserFromApi = function( options ){
  76. var currentUser = new User();
  77. currentUser.loadFromApi( User.CURRENT_ID_STR, options );
  78. return currentUser;
  79. };
  80. // (stub) collection for users (shouldn't be common unless admin UI)
  81. var UserCollection = Backbone.Collection.extend( LoggableMixin ).extend({
  82. model : User,
  83. urlRoot : 'api/users'
  84. //logger : console,
  85. });