Guest User

Untitled

a guest
Oct 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1.  
  2. var tket = {
  3. Views: {},
  4. Models: {},
  5. Collections: {},
  6. seller: '',
  7. vent: _.extend({}, Backbone.Events),
  8. };
  9.  
  10. tket.Models.Event = Backbone.Model.extend({});
  11.  
  12. tket.Collections.Event = Backbone.Collection.extend({
  13. model: tket.Models.Event,
  14. url: "/event/",
  15. initialize: function (d) {
  16. this.url = this.url+tket.seller;
  17. }
  18. });
  19.  
  20.  
  21. tket.Models.PusherModel = Backbone.Model.extend({});
  22. tket.Collections.Pusher = Backbone.Collection.extend({
  23. windowIsActive: null,
  24. editing: null,
  25. model: PusherModel,
  26. time: null,
  27. _stop: false,
  28. initialize: function() {
  29. var that = this;
  30. this.windowIsActive = true;
  31. this.editing = false;
  32. this.time = 5000;
  33. this._stop = false;
  34. window.onfocus = function () {
  35. that.windowIsActive = true;
  36. };
  37. window.onblur = function () {
  38. that.windowIsActive = false;
  39. };
  40. this.run();
  41.  
  42. return this;
  43. },
  44. run: function() {
  45. var that = this;
  46. var internalRun = function() {
  47. if(that.windowIsActive && !that.editing ) {
  48. _(that.models).each(function(model) {
  49. console.log(model.get('e'));
  50. tket.vent.trigger(model.get('e'));
  51. }, that);
  52. };
  53. if(!that._stop) {
  54. window.setTimeout(internalRun, that.time);
  55. }
  56. };
  57. window.setTimeout(internalRun, that.time);
  58. },
  59. setEditing: function(editing) {
  60. this.editing = editing;
  61. return this;
  62. },
  63. setTime: function(time) {
  64. this.time = time;
  65. return this;
  66. },
  67. stop: function() {
  68. this._stop = true;
  69. return this;
  70. },
  71. start: function() {
  72. if(this._stop) {
  73. this._stop = false;
  74. window.setTimeout(this.run, this.time*2);
  75. }
  76. return this;
  77. },
  78. addP: function(trigger_event, cCollection) {
  79. var m = this.models.filter(function(model){
  80. return model.attributes.e == trigger_event;
  81. });
  82. if(!m || !m.length) {
  83. var that = this;
  84. if(cCollection) {
  85. on = tket.vent.on(trigger_event, function() {
  86. that.updateCollection(cCollection);
  87. });
  88. } else {
  89. on = false;
  90. }
  91. this.add({e:trigger_event, on:on});
  92. }
  93. return this;
  94. },
  95. removeP: function(trigger_event) {
  96. var that = this;
  97. _(that.models).each(function(model) {
  98. var model_e = model.get('e');
  99. if ( _.isEqual(model_e, trigger_event) == true) {
  100. that.remove(model);
  101. }
  102. }, that);
  103. tket.vent.unbind(trigger_event);
  104. return this;
  105. },
  106. updateCollection: function(collection) {
  107. var that = this;
  108. var newCollection = new collection.constructor();
  109. newCollection.url = collection.url;
  110. newCollection.model = collection.model;
  111. newCollection.fetch({
  112. success: function() {
  113. var curModelIds = that.getIdsOfModels(collection.models);
  114. var newModelIds = that.getIdsOfModels(newCollection.models);
  115.  
  116. _(newCollection.models).each(function(newModel) {
  117. if (curModelIds.indexOf(newModel.id) == -1) {
  118. collection.add(newModel);
  119. }
  120. }, that);
  121.  
  122. _(collection.models).each(function(curModel) {
  123. if (newModelIds.indexOf(curModel.id) == -1) {
  124. collection.remove(curModel);
  125. } else {
  126. var curModelAttributes = curModel.attributes;
  127. var newModelAttributes = newCollection.get(curModel.id).attributes;
  128. if ( _.isEqual(curModelAttributes, newModelAttributes) == false) {
  129. curModel.set(newModelAttributes);
  130. }
  131. }
  132. }, that);
  133. }
  134. });
  135. },
  136. getIdsOfModels: function(models) {
  137. return _(models).map(function(model) { return model.id; });
  138. },
  139. });
  140. tket.pusher = new tket.Collections.Pusher();
  141. tket.events = new tket.Collections.Event();
  142.  
  143.  
  144. tket.pusher.addP('collection:event:poll', tket.events).setTime(5000);
Add Comment
Please, Sign In to add comment