/src/js/cilantro/models/view.js

https://github.com/cbmi/cilantro · JavaScript · 148 lines · 110 code · 33 blank · 5 comment · 13 complexity · ed47a1082783bbc5eff3fec628f0643d MD5 · raw file

  1. /* global define */
  2. define([
  3. 'underscore',
  4. 'backbone',
  5. '../core',
  6. './base'
  7. ], function(_, Backbone, c, base) {
  8. var Facet = Backbone.Model.extend({
  9. idAttribute: 'concept'
  10. });
  11. var Facets = Backbone.Collection.extend({
  12. model: Facet
  13. });
  14. var ViewModel = base.Model.extend({
  15. constructor: function() {
  16. this.facets = new Facets();
  17. // HACK: Convert columns in facets with the specific sets of
  18. // models. This is until the facets API is supported on the server.
  19. this.on('change:json', function(model, value) {
  20. return this.jsonToFacets(value);
  21. });
  22. base.Model.prototype.constructor.apply(this, arguments);
  23. },
  24. initialize: function() {
  25. var _this = this;
  26. this.on('request', function() {
  27. return c.trigger(c.VIEW_SYNCING, this);
  28. });
  29. this.on('sync', function(model, attrs, options) {
  30. if (!options) options = {};
  31. if (options.silent !== true) {
  32. return c.trigger(c.VIEW_SYNCED, this, 'success');
  33. }
  34. });
  35. this.on('error', function() {
  36. return c.trigger(c.VIEW_SYNCED, this, 'error');
  37. });
  38. this.on('change', function() {
  39. return c.trigger(c.VIEW_CHANGED, this);
  40. });
  41. return c.on(c.VIEW_SAVE, function(id) {
  42. if (_this.id === id || !id && _this.isSession()) {
  43. return _this.save();
  44. }
  45. });
  46. },
  47. isSession: function() {
  48. return this.get('session');
  49. },
  50. isArchived: function() {
  51. return this.get('archived');
  52. },
  53. toJSON: function() {
  54. var attrs = base.Model.prototype.toJSON.apply(this, arguments);
  55. attrs.json = this.facetsToJSON();
  56. return attrs;
  57. },
  58. parse: function(attrs) {
  59. base.Model.prototype.parse.apply(this, arguments);
  60. this.jsonToFacets(attrs.json);
  61. return attrs;
  62. },
  63. jsonToFacets: function(json) {
  64. if (!json) json = {};
  65. // Implies this is an array of object, set directly. This is for
  66. // forwards compatibility.
  67. if (_.isArray(json)) {
  68. this.facets.reset(json);
  69. return;
  70. }
  71. var attrs, id, order;
  72. var models = [];
  73. var columns = json.columns || [];
  74. var ordering = json.ordering || [];
  75. for (var i = 0; i < columns.length; i++) {
  76. id = columns[i];
  77. attrs = {
  78. concept: id
  79. };
  80. for (var j = 0; j < ordering.length; j++) {
  81. order = ordering[j];
  82. if (id === order[0]) {
  83. attrs.sort = order[1];
  84. attrs.sort_index = j; // jshint ignore:line
  85. }
  86. }
  87. models.push(attrs);
  88. }
  89. return this.facets.reset(models);
  90. },
  91. facetsToJSON: function() {
  92. var json = {
  93. ordering: [],
  94. columns: []
  95. };
  96. this.facets.each(function(model) {
  97. var direction, index, sort;
  98. json.columns.push(model.get('concept'));
  99. if ((direction = model.get('sort'))) {
  100. index = model.get('sort_index');
  101. sort = [model.get('concept'), direction];
  102. return json.ordering.splice(index, 0, sort);
  103. }
  104. });
  105. return json;
  106. }
  107. });
  108. var ViewCollection = base.SessionCollection.extend({
  109. model: ViewModel
  110. });
  111. return {
  112. ViewModel: ViewModel,
  113. ViewCollection: ViewCollection
  114. };
  115. });