EventInstanceDataSource.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. var EventInstanceDataSource = Class.extend(EmitterMixin, {
  2. instanceRepo: null,
  3. freezeDepth: 0,
  4. outboundChangeset: null,
  5. isPopulated: false,
  6. constructor: function() {
  7. this.instanceRepo = new EventInstanceRepo();
  8. },
  9. tryReset: function() {
  10. if (this.isPopulated && this.canTrigger()) {
  11. this.triggerChangeset(new EventInstanceChangeset(
  12. true, // isClear
  13. null, // removals
  14. this.instanceRepo // additions
  15. ));
  16. }
  17. },
  18. // Reporting and Triggering
  19. // -----------------------------------------------------------------------------------------------------------------
  20. addChangeset: function(changeset) {
  21. if (!this.outboundChangeset) {
  22. this.outboundChangeset = new EventInstanceChangeset();
  23. }
  24. changeset.applyToChangeset(this.outboundChangeset);
  25. this.trySendOutbound();
  26. },
  27. freeze: function() {
  28. this.freezeDepth++;
  29. },
  30. thaw: function() {
  31. this.freezeDepth--;
  32. this.trySendOutbound();
  33. },
  34. trySendOutbound: function() { // also might apply outbound changes to INTERNAL data
  35. var outboundChangeset = this.outboundChangeset;
  36. if (this.canTrigger()) {
  37. this.isPopulated = true; // event if empty result, consider populated
  38. if (outboundChangeset) {
  39. outboundChangeset.applyToRepo(this.instanceRepo); // finally internally record
  40. this.outboundChangeset = null;
  41. this.triggerChangeset(outboundChangeset);
  42. }
  43. else {
  44. // hack for eventAfterAllRender
  45. // also for DateComponents to know an empy, but populated, state
  46. this.triggerChangeset(new EventInstanceChangeset());
  47. }
  48. }
  49. },
  50. canTrigger: function() {
  51. return !this.freezeDepth;
  52. },
  53. triggerChangeset: function(changeset) {
  54. this.trigger('before:receive');
  55. this.trigger('receive', changeset);
  56. this.trigger('after:receive');
  57. }
  58. });