PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/go/base/static/js/src/routing/models.js

https://github.com/praekelt/vumi-go
JavaScript | 186 lines | 151 code | 29 blank | 6 comment | 0 complexity | ca1e0381a7a2288bb3eb363271d01f45 MD5 | raw file
  1. // go.routing (models)
  2. // ===================
  3. // Models for routing screen.
  4. // We use the state machine models as a base to make use of any
  5. // custom/overriden functionality the models provide over Backbone.Model,
  6. // seeing as the models for routing are of a state machine nature.
  7. (function(exports) {
  8. var stateMachine = go.components.stateMachine,
  9. EndpointModel = stateMachine.EndpointModel,
  10. ConnectionModel = stateMachine.ConnectionModel,
  11. StateModel = stateMachine.StateModel,
  12. StateMachineModel = stateMachine.StateMachineModel;
  13. var RoutingEndpointModel = EndpointModel.extend({});
  14. var RoutingEntryModel = ConnectionModel.extend({
  15. relations: [{
  16. type: Backbone.HasOne,
  17. key: 'source',
  18. includeInJSON: ['uuid'],
  19. relatedModel: RoutingEndpointModel
  20. }, {
  21. type: Backbone.HasOne,
  22. key: 'target',
  23. includeInJSON: ['uuid'],
  24. relatedModel: RoutingEndpointModel
  25. }]
  26. });
  27. var RoutingStateCollection = Backbone.Collection.extend({
  28. type: null,
  29. bindings: {
  30. 'add': function(model) {
  31. model.set('ordinal', this.localOrdinals.indexOf(model.id));
  32. }
  33. },
  34. initialize: function(models, options) {
  35. this.routingId = options.routingId;
  36. this.localOrdinals = go.local.get(this._localOrdinalsKey(), []);
  37. go.utils.bindEvents(this.bindings, this);
  38. },
  39. persistOrdinals: function() {
  40. go.local.set(this._localOrdinalsKey(), this._getOrdinals());
  41. },
  42. _getOrdinals: function() {
  43. return _.pluck(this.sortBy('ordinal'), 'id');
  44. },
  45. _localOrdinalsKey: function() {
  46. return localOrdinalsKey(this.routingId, this.type);
  47. }
  48. });
  49. var ChannelCollection = RoutingStateCollection.extend({
  50. type: 'channels'
  51. });
  52. var RouterCollection = RoutingStateCollection.extend({
  53. type: 'routers'
  54. });
  55. var ConversationCollection = RoutingStateCollection.extend({
  56. type: 'conversations'
  57. });
  58. var RoutingStateModel = StateModel.extend({
  59. defaults: {ordinal: -1}
  60. });
  61. var ChannelModel = RoutingStateModel.extend({
  62. relations: [{
  63. type: Backbone.HasMany,
  64. key: 'endpoints',
  65. relatedModel: RoutingEndpointModel
  66. }],
  67. viewURL: function(){
  68. return '/channels/' + encodeURIComponent(this.get('tag').join(':')) + '/';
  69. }
  70. });
  71. var RouterModel = RoutingStateModel.extend({
  72. relations: [{
  73. type: Backbone.HasMany,
  74. key: 'conversation_endpoints',
  75. relatedModel: RoutingEndpointModel
  76. }, {
  77. type: Backbone.HasMany,
  78. key: 'channel_endpoints',
  79. relatedModel: RoutingEndpointModel
  80. }],
  81. viewURL: function(){
  82. return '/routers/' + encodeURI(this.id) + '/edit/';
  83. }
  84. });
  85. var ConversationModel = RoutingStateModel.extend({
  86. relations: [{
  87. type: Backbone.HasMany,
  88. key: 'endpoints',
  89. relatedModel: RoutingEndpointModel
  90. }],
  91. viewURL: function(){
  92. return '/conversations/' + encodeURI(this.id) + '/edit/';
  93. }
  94. });
  95. var RoutingModel = StateMachineModel.extend({
  96. methods: {
  97. read: {
  98. method: 'routing_table',
  99. params: ['campaign_id']
  100. },
  101. update: {
  102. method: 'update_routing_table',
  103. params: function() {
  104. return [this.get('campaign_id'), this];
  105. }
  106. }
  107. },
  108. idAttribute: 'campaign_id',
  109. relations: [{
  110. type: Backbone.HasMany,
  111. key: 'channels',
  112. relatedModel: ChannelModel,
  113. collectionType: ChannelCollection,
  114. collectionOptions: stateCollectionOptions
  115. }, {
  116. type: Backbone.HasMany,
  117. key: 'routers',
  118. relatedModel: RouterModel,
  119. collectionType: RouterCollection,
  120. collectionOptions: stateCollectionOptions
  121. }, {
  122. type: Backbone.HasMany,
  123. key: 'conversations',
  124. relatedModel: ConversationModel,
  125. collectionType: ConversationCollection,
  126. collectionOptions: stateCollectionOptions
  127. }, {
  128. type: Backbone.HasMany,
  129. key: 'routing_entries',
  130. parse: true,
  131. relatedModel: RoutingEntryModel
  132. }],
  133. persistOrdinals: function() {
  134. this.get('channels').persistOrdinals();
  135. this.get('routers').persistOrdinals();
  136. this.get('conversations').persistOrdinals();
  137. this.trigger('persist:ordinals');
  138. }
  139. });
  140. function stateCollectionOptions(routing) {
  141. return {routingId: routing.id};
  142. }
  143. function localOrdinalsKey(id, type) {
  144. return [id, type, 'ordinals'].join(':');
  145. }
  146. _.extend(exports, {
  147. RoutingModel: RoutingModel,
  148. RoutingStateCollection: RoutingStateCollection,
  149. RoutingStateModel: RoutingStateModel,
  150. ChannelModel: ChannelModel,
  151. RouterModel: RouterModel,
  152. ConversationModel: ConversationModel,
  153. RoutingEntryModel: RoutingEntryModel,
  154. RoutingEndpointModel: RoutingEndpointModel,
  155. localOrdinalsKey: localOrdinalsKey
  156. });
  157. })(go.routing.models = {});