PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/client/galaxy/scripts/mvc/visualization/visualization-model.js

https://bitbucket.org/remy_d1/galaxy-central-manageapi
JavaScript | 142 lines | 59 code | 21 blank | 62 comment | 9 complexity | f4d8527c3e7d8ff0099552bef2c5118b MD5 | raw file
Possible License(s): CC-BY-3.0
  1. //define([
  2. //], function(){
  3. //==============================================================================
  4. /** @class Model for a saved Galaxy visualization.
  5. *
  6. * @augments Backbone.Model
  7. * @constructs
  8. */
  9. var Visualization = Backbone.Model.extend(
  10. /** @lends Visualization.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. /** default attributes for a model */
  15. defaults : {
  16. config : {}
  17. },
  18. /** override urlRoot to handle presence/absence of galaxy_config */
  19. urlRoot : function(){
  20. var apiUrl = '/api/visualizations';
  21. return (( window.galaxy_config && galaxy_config.root )?( galaxy_config.root + apiUrl ):( apiUrl ));
  22. },
  23. /** Set up the model, determine if accessible, bind listeners
  24. * @see Backbone.Model#initialize
  25. */
  26. initialize : function( data ){
  27. //this.log( this + '.initialize', data, this.attributes );
  28. // munge config sub-object here since bbone won't handle defaults with this
  29. if( _.isObject( data.config ) && _.isObject( this.defaults.config ) ){
  30. _.defaults( data.config, this.defaults.config );
  31. }
  32. this._setUpListeners();
  33. },
  34. /** set up any event listeners
  35. */
  36. _setUpListeners : function(){
  37. //this.on( 'change', function(){
  38. // console.info( 'change:', arguments );
  39. //});
  40. },
  41. // ........................................................................ config
  42. /** override set to properly allow update and trigger change when setting the sub-obj 'config' */
  43. set: function( key, val ){
  44. //TODO: validate config is object
  45. if( key === 'config' ){
  46. var oldConfig = this.get( 'config' );
  47. // extend if already exists (is this correct behavior? no way to eliminate keys or reset entirely)
  48. // clone in order to trigger change (diff. obj ref)
  49. if( _.isObject( oldConfig ) ){
  50. val = _.extend( _.clone( oldConfig ), val );
  51. }
  52. }
  53. Backbone.Model.prototype.set.call( this, key, val );
  54. return this;
  55. },
  56. // ........................................................................ misc
  57. /** String representation */
  58. toString : function(){
  59. var idAndTitle = this.get( 'id' ) || '';
  60. if( this.get( 'title' ) ){
  61. idAndTitle += ':' + this.get( 'title' );
  62. }
  63. return 'Visualization(' + idAndTitle + ')';
  64. }
  65. });
  66. //==============================================================================
  67. /** @class Backbone collection of visualization models
  68. *
  69. * @borrows LoggableMixin#logger as #logger
  70. * @borrows LoggableMixin#log as #log
  71. * @constructs
  72. */
  73. var VisualizationCollection = Backbone.Collection.extend(
  74. /** @lends VisualizationCollection.prototype */{
  75. model : Visualization,
  76. ///** logger used to record this.log messages, commonly set to console */
  77. //// comment this out to suppress log output
  78. //logger : console,
  79. url : function(){
  80. return galaxy_config.root + 'api/visualizations';
  81. },
  82. /** Set up.
  83. * @see Backbone.Collection#initialize
  84. */
  85. initialize : function( models, options ){
  86. options = options || {};
  87. //this._setUpListeners();
  88. },
  89. //_setUpListeners : function(){
  90. //},
  91. // ........................................................................ common queries
  92. // ........................................................................ ajax
  93. // ........................................................................ misc
  94. set : function( models, options ){
  95. // arrrrrrrrrrrrrrrrrg...
  96. // override to get a correct/smarter merge when incoming data is partial (e.g. stupid backbone)
  97. // w/o this partial models from the server will fill in missing data with model defaults
  98. // and overwrite existing data on the client
  99. // see Backbone.Collection.set and _prepareModel
  100. var collection = this;
  101. models = _.map( models, function( model ){
  102. var existing = collection.get( model.id );
  103. if( !existing ){ return model; }
  104. // merge the models _BEFORE_ calling the superclass version
  105. var merged = existing.toJSON();
  106. _.extend( merged, model );
  107. return merged;
  108. });
  109. // now call superclass when the data is filled
  110. Backbone.Collection.prototype.set.call( this, models, options );
  111. },
  112. /** String representation. */
  113. toString : function(){
  114. return ([ 'VisualizationCollection(', [ this.historyId, this.length ].join(), ')' ].join( '' ));
  115. }
  116. });
  117. //==============================================================================
  118. //return {
  119. // Visualization : Visualization,
  120. // VisualizationCollection : VisualizationCollection
  121. //};});