Notifier.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. --[[
  2. * @author PureMVC Lua Native Port by Sean
  3. * @author Copyright(c) 2006-2012 Futurescale, Inc., Some rights reserved.
  4. *
  5. * @class puremvc.Notifier
  6. *
  7. * A Base Notifier implementation.
  8. *
  9. * {@link puremvc.MacroCommand MacroCommand},
  10. * {@link puremvc.SimpleCommand SimpleCommand},
  11. * {@link puremvc.Mediator Mediator} and
  12. * {@link puremvc.Proxy Proxy}
  13. * all have a need to send Notifications
  14. *
  15. * The Notifier interface provides a common method called #sendNotification that
  16. * relieves implementation code of the necessity to actually construct
  17. * Notifications.
  18. *
  19. * The Notifier class, which all of the above mentioned classes
  20. * extend, provides an initialized reference to the
  21. * {@link puremvc.Facade Facade}
  22. * Multiton, which is required for the convienience method
  23. * for sending Notifications but also eases implementation as these
  24. * classes have frequent
  25. * {@link puremvc.Facade Facade} interactions
  26. * and usually require access to the facade anyway.
  27. *
  28. * NOTE: In the MultiCore version of the framework, there is one caveat to
  29. * notifiers, they cannot send notifications or reach the facade until they
  30. * have a valid multitonKey.
  31. *
  32. * The multitonKey is set:
  33. * - on a Command when it is executed by the Controller
  34. * - on a Mediator is registered with the View
  35. * - on a Proxy is registered with the Model.
  36. *
  37. * @constructor
  38. ]]
  39. local Facade = require( framework.PACKAGE_NAME .. '/patterns/facade/Facade')
  40. local Notifier = class("Notifier")
  41. function Notifier:ctor()
  42. end
  43. --[[
  44. * Create and send a Notification.
  45. *
  46. * Keeps us from having to construct new Notification instances in our
  47. * implementation code.
  48. *
  49. * @param {string} notificationName
  50. * A notification name
  51. * @param {Object} [body]
  52. * The body of the notification
  53. * @param {string} [type]
  54. * The notification type
  55. * @return {void}
  56. ]]
  57. function Notifier:sendNotification(notificationName, body, type)
  58. local facade = self.facade
  59. if facade ~= nil then
  60. facade:sendNotification(notificationName, body, type)
  61. end
  62. end
  63. --[[
  64. * Initialize this Notifier instance.
  65. *
  66. * This is how a Notifier gets its multitonKey.
  67. * Calls to #sendNotification or to access the
  68. * facade will fail until after this method
  69. * has been called.
  70. *
  71. * Mediators, Commands or Proxies may override
  72. * this method in order to send notifications
  73. * or access the Multiton Facade instance as
  74. * soon as possible. They CANNOT access the facade
  75. * in their constructors, since this method will not
  76. * yet have been called.
  77. *
  78. *
  79. * @param {string} key
  80. * The Notifiers multiton key;
  81. * @return {void}
  82. ]]
  83. function Notifier:initializeNotifier(key)
  84. self.multitonKey = key
  85. self.facade = self:getFacade()
  86. end
  87. --[[
  88. * Retrieve the Multiton Facade instance
  89. *
  90. *
  91. * @protected
  92. * @return {puremvc.Facade}
  93. ]]
  94. function Notifier:getFacade()
  95. if self.multitonKey == nil then
  96. error(Notifier.MULTITON_MSG)
  97. end
  98. return Facade.getInstance(self.multitonKey)
  99. end
  100. Notifier.MULTITON_MSG = "multitonKey for this Notifier not yet initialized!";
  101. return Notifier