PageRenderTime 77ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/hbc/galaxy-central-hbc/
JavaScript | 250 lines | 124 code | 40 blank | 86 comment | 7 complexity | 60f32c5960ce906cc15c30ae29dce91d MD5 | raw file
Possible License(s): CC-BY-3.0
  1. define([
  2. "mvc/dataset/hda-model",
  3. "mvc/base-mvc",
  4. "utils/localization"
  5. ], function( HDA_MODEL, BASE_MVC, _l ){
  6. //==============================================================================
  7. /** @class Backbone model for Dataset collection elements.
  8. * DC Elements contain a sub-model named 'object'. This class moves that
  9. * 'object' from the JSON in the attributes list to a full, instantiated
  10. * sub-model found in this.object. This is done on intialization and
  11. * everytime the 'change:object' event is fired.
  12. *
  13. * @borrows LoggableMixin#logger as #logger
  14. * @borrows LoggableMixin#log as #log
  15. * @constructs
  16. */
  17. var DatasetCollectionElement = Backbone.Model.extend( BASE_MVC.LoggableMixin ).extend(
  18. /** @lends DatasetCollectionElement.prototype */{
  19. //TODO:?? this model may be unneccessary - it reflects the api structure, but...
  20. // if we munge the element with the element.object at parse, we can flatten the entire hierarchy
  21. /** logger used to record this.log messages, commonly set to console */
  22. // comment this out to suppress log output
  23. //logger : console,
  24. defaults : {
  25. id : null,
  26. model_class : 'DatasetCollectionElement',
  27. element_identifier : null,
  28. element_index : null,
  29. element_type : null
  30. },
  31. /** Set up.
  32. * @see Backbone.Collection#initialize
  33. */
  34. initialize : function( model, options ){
  35. this.info( this + '.initialize:', model, options );
  36. options = options || {};
  37. //this._setUpListeners();
  38. this.object = this._createObjectModel();
  39. this.on( 'change:object', function(){
  40. //console.log( 'change:object' );
  41. //TODO: prob. better to update the sub-model instead of re-creating it
  42. this.object = this._createObjectModel();
  43. });
  44. },
  45. _createObjectModel : function(){
  46. //console.log( '_createObjectModel', this.get( 'object' ), this.object );
  47. //TODO: same patterns as HDCA _createElementsModel - refactor to BASE_MVC.hasSubModel?
  48. if( _.isUndefined( this.object ) ){ this.object = null; }
  49. if( !this.get( 'object' ) ){ return this.object; }
  50. var object = this.get( 'object' );
  51. this.unset( 'object', { silent: true });
  52. this.debug( 'DCE, element_type:', this.get( 'element_type' ) );
  53. switch( this.get( 'element_type' ) ){
  54. case 'dataset_collection':
  55. this.object = new DatasetCollection( object );
  56. break;
  57. case 'hda':
  58. this.object = new HDA_MODEL.HistoryDatasetAssociation( object );
  59. break;
  60. default:
  61. throw new TypeError( 'Unknown element_type: ' + this.get( 'element_type' ) );
  62. }
  63. return this.object;
  64. },
  65. /** String representation. */
  66. toString : function(){
  67. var objStr = ( this.object )?( '' + this.object ):( this.get( 'element_identifier' ) );
  68. return ([ 'DatasetCollectionElement(', objStr, ')' ].join( '' ));
  69. }
  70. });
  71. //==============================================================================
  72. /** @class Backbone collection for DCEs.
  73. * NOTE: used *only* in second level of list:paired collections (a
  74. * collection that contains collections)
  75. *
  76. * @borrows LoggableMixin#logger as #logger
  77. * @borrows LoggableMixin#log as #log
  78. * @constructs
  79. */
  80. var DatasetCollectionElementCollection = Backbone.Collection.extend( BASE_MVC.LoggableMixin ).extend(
  81. /** @lends DatasetCollectionElementCollection.prototype */{
  82. model: DatasetCollectionElement,
  83. // comment this out to suppress log output
  84. /** logger used to record this.log messages, commonly set to console */
  85. //logger : console,
  86. /** Set up.
  87. * @see Backbone.Collection#initialize
  88. */
  89. initialize : function( models, options ){
  90. options = options || {};
  91. this.info( this + '.initialize:', models, options );
  92. //this._setUpListeners();
  93. },
  94. /** String representation. */
  95. toString : function(){
  96. return ([ 'DatasetCollectionElementCollection(', this.length, ')' ].join( '' ));
  97. }
  98. });
  99. //==============================================================================
  100. /** @class Backbone model for Dataset Collections.
  101. * DCs contain a bbone collection named 'elements' using the class found in
  102. * this.collectionClass (gen. DatasetCollectionElementCollection). DCs move
  103. * that 'object' from the JSON in the attributes list to a full, instantiated
  104. * collection found in this.elements. This is done on intialization and
  105. * everytime the 'change:elements' event is fired.
  106. *
  107. * @borrows LoggableMixin#logger as #logger
  108. * @borrows LoggableMixin#log as #log
  109. * @constructs
  110. */
  111. var DatasetCollection = Backbone.Model.extend( BASE_MVC.LoggableMixin ).extend(
  112. /** @lends ListDatasetCollection.prototype */{
  113. //logger : console,
  114. /** default attributes for a model */
  115. defaults : {
  116. collection_type : 'list'
  117. },
  118. collectionClass : DatasetCollectionElementCollection,
  119. /** */
  120. initialize : function( model, options ){
  121. this.info( 'DatasetCollection.initialize:', model, options );
  122. //historyContent.HistoryContent.prototype.initialize.call( this, attrs, options );
  123. this.elements = this._createElementsModel();
  124. //TODO:?? no way to use parse here?
  125. this.on( 'change:elements', function(){
  126. this.log( 'change:elements' );
  127. //TODO: prob. better to update the collection instead of re-creating it
  128. this.elements = this._createElementsModel();
  129. });
  130. },
  131. /** move elements model attribute to full collection */
  132. _createElementsModel : function(){
  133. this.log( '_createElementsModel', this.get( 'elements' ), this.elements );
  134. //TODO: same patterns as DatasetCollectionElement _createObjectModel - refactor to BASE_MVC.hasSubModel?
  135. var elements = this.get( 'elements' ) || [];
  136. this.info( 'elements:', elements );
  137. this.unset( 'elements', { silent: true });
  138. this.elements = new this.collectionClass( elements );
  139. return this.elements;
  140. },
  141. hasDetails : function(){
  142. //TODO: this is incorrect for (accidentally) empty collections
  143. return this.elements.length !== 0;
  144. },
  145. // ........................................................................ misc
  146. /** String representation */
  147. toString : function(){
  148. var idAndName = [ this.get( 'id' ), this.get( 'name' ) || this.get( 'element_identifier' ) ];
  149. return 'DatasetCollection(' + ( idAndName.join(',') ) + ')';
  150. }
  151. });
  152. //==============================================================================
  153. /** @class Backbone collection for a collection of collection collections collecting correctly. */
  154. var DatasetCollectionCollection = Backbone.Collection.extend( BASE_MVC.LoggableMixin ).extend({
  155. model: DatasetCollection,
  156. ///** logger used to record this.log messages, commonly set to console */
  157. //// comment this out to suppress log output
  158. //logger : console,
  159. /** Set up.
  160. * @see Backbone.Collection#initialize
  161. */
  162. initialize : function( models, options ){
  163. options = options || {};
  164. this.info( 'DatasetCollectionCollection.initialize:', models, options );
  165. //this._setUpListeners();
  166. },
  167. /** String representation. */
  168. toString : function(){
  169. return ([ 'DatasetCollectionCollection(', this.get( 'name' ), ')' ].join( '' ));
  170. }
  171. });
  172. //NOTE: the following prototypes may not be necessary - but I wanted to specifiy
  173. // them (for now) and allow for the possibility of unique functionality
  174. //==============================================================================
  175. var ListDatasetCollection = DatasetCollection.extend(
  176. /** @lends ListDatasetCollection.prototype */{
  177. /** String representation. */
  178. toString : function(){
  179. return ([ 'ListDatasetCollection(', this.get( 'name' ), ')' ].join( '' ));
  180. }
  181. });
  182. //==============================================================================
  183. var PairDatasetCollection = DatasetCollection.extend(
  184. /** @lends ListDatasetCollection.prototype */{
  185. /** String representation. */
  186. toString : function(){
  187. return ([ 'PairDatasetCollection(', this.get( 'name' ), ')' ].join( '' ));
  188. }
  189. });
  190. //==============================================================================
  191. var ListPairedDatasetCollection = DatasetCollection.extend(
  192. /** @lends ListDatasetCollection.prototype */{
  193. // list:paired is the only collection that itself contains collections
  194. //collectionClass : DatasetCollectionCollection,
  195. /** String representation. */
  196. toString : function(){
  197. return ([ 'ListPairedDatasetCollection(', this.get( 'name' ), ')' ].join( '' ));
  198. }
  199. });
  200. //==============================================================================
  201. return {
  202. DatasetCollectionElement : DatasetCollectionElement,
  203. DatasetCollectionElementCollection : DatasetCollectionElementCollection,
  204. DatasetCollection : DatasetCollection,
  205. DatasetCollectionCollection : DatasetCollectionCollection,
  206. ListDatasetCollection : ListDatasetCollection,
  207. PairDatasetCollection : PairDatasetCollection,
  208. ListPairedDatasetCollection : ListPairedDatasetCollection
  209. };
  210. });