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

https://bitbucket.org/cistrome/cistrome-harvard/ · JavaScript · 111 lines · 71 code · 18 blank · 22 comment · 4 complexity · 0537e700d22f1b044e9d917dafe91f3a MD5 · raw file

  1. // dependencies
  2. define([], function() {
  3. // LIBRARY
  4. var Library = Backbone.Model.extend({
  5. urlRoot: '/api/libraries/',
  6. /** based on show_deleted
  7. * would this lib show in the list of lib's?
  8. * @param {Boolean} show_deleted are we showing deleted libraries?
  9. */
  10. isVisible : function(show_deleted){
  11. var isVisible = true;
  12. if( (!show_delete) && (this.get('deleted')) ){
  13. isVisible = false;
  14. }
  15. return isVisible;
  16. }
  17. });
  18. // FOLDER AS MODEL
  19. var FolderAsModel = Backbone.Model.extend({
  20. urlRoot: '/api/folders'
  21. });
  22. // LIBRARIES
  23. var Libraries = Backbone.Collection.extend({
  24. url: '/api/libraries',
  25. model: Library,
  26. sort_key: 'name', // default
  27. sort_order: null, // default
  28. initialize : function(options){
  29. options = options || {};
  30. },
  31. /** Get every 'shown' library in this collection based on deleted filter
  32. * @param {Boolean} show_deleted are we showing deleted libraries?
  33. * @returns array of library models
  34. */
  35. getVisible : function(show_deleted, filters){
  36. filters = filters || [];
  37. // always filter by show deleted first
  38. var filteredLibraries = new Libraries( this.filter( function( item ){
  39. return item.isVisible(show_deleted);
  40. }));
  41. return filteredLibraries;
  42. }
  43. });
  44. // ITEM
  45. var Item = Backbone.Model.extend({
  46. urlRoot : '/api/libraries/datasets'
  47. });
  48. // FOLDER AS COLLECTION
  49. var Folder = Backbone.Collection.extend({
  50. model: Item
  51. });
  52. // CONTAINER for folder contents (folders, items and metadata).
  53. var FolderContainer = Backbone.Model.extend({
  54. defaults : {
  55. folder : new Folder(),
  56. full_path : "unknown",
  57. urlRoot : "/api/folders/",
  58. id : "unknown"
  59. },
  60. parse : function(obj) {
  61. this.full_path = obj[0].full_path;
  62. // response is not a simple array, it contains metadata
  63. // this will update the inner collection
  64. this.get("folder").reset(obj[1].folder_contents);
  65. return obj;
  66. }
  67. });
  68. // HISTORY ITEM
  69. var HistoryItem = Backbone.Model.extend({
  70. urlRoot : '/api/histories/'
  71. });
  72. // HISTORY
  73. var GalaxyHistory = Backbone.Model.extend({
  74. url : '/api/histories/'
  75. });
  76. // HISTORIES
  77. var GalaxyHistories = Backbone.Collection.extend({
  78. url : '/api/histories',
  79. model : GalaxyHistory
  80. });
  81. // return
  82. return {
  83. Library: Library,
  84. FolderAsModel : FolderAsModel,
  85. Libraries : Libraries,
  86. Item : Item,
  87. Folder : Folder,
  88. FolderContainer : FolderContainer,
  89. HistoryItem : HistoryItem,
  90. GalaxyHistory : GalaxyHistory,
  91. GalaxyHistories : GalaxyHistories
  92. };
  93. });