Notification.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. --[[
  2. * @author PureMVC Lua Native Port by Sean
  3. * @author Copyright(c) 2006-2012 Futurescale, Inc., Some rights reserved.
  4. *
  5. * @class puremvc.Notification
  6. *
  7. * A base Notification implementation.
  8. *
  9. * PureMVC does not rely upon underlying event models such as the one provided
  10. * with the DOM or other browser centric W3C event models.
  11. *
  12. * The Observer Pattern as implemented within PureMVC exists to support
  13. * event-driven communication between the application and the actors of the MVC
  14. * triad.
  15. *
  16. * Notifications are not meant to be a replacement for events in the browser.
  17. * Generally, Mediator implementors place event listeners on their view
  18. * components, which they then handle in the usual way. This may lead to the
  19. * broadcast of Notifications to trigger commands or to communicate with other
  20. * Mediators. {@link puremvc.Proxy Proxy},
  21. * {@link puremvc.SimpleCommand SimpleCommand}
  22. * and {@link puremvc.MacroCommand MacroCommand}
  23. * instances communicate with each other and
  24. * {@link puremvc.Mediator Mediator}s
  25. * by broadcasting Notifications.
  26. *
  27. * A key difference between browser events and PureMVC Notifications is that
  28. * events follow the 'Chain of Responsibility' pattern, 'bubbling' up the
  29. * display hierarchy until some parent component handles the event, while
  30. * PureMVC Notification follow a 'Publish/Subscribe' pattern. PureMVC classes
  31. * need not be related to each other in a parent/child relationship in order to
  32. * communicate with one another using Notifications.
  33. *
  34. * @constructor
  35. * @param {string} name
  36. * The Notification name
  37. * @param {Object} [body]
  38. * The Notification body
  39. * @param {Object} [type]
  40. * The Notification type
  41. ]]
  42. local Notification = class("Notification")
  43. function Notification:ctor(name, body, type)
  44. self.name = name
  45. self.body = body
  46. self.type = type
  47. end
  48. --[[
  49. * Get the name of the Notification instance
  50. *
  51. * @return {string}
  52. * The name of the Notification instance
  53. ]]
  54. function Notification:getName()
  55. return self.name
  56. end
  57. --[[
  58. * Set this Notifications body.
  59. * @param {Object} body
  60. * @return {void}
  61. ]]
  62. function Notification:setBody(body)
  63. self.body = body
  64. end
  65. --[[
  66. * Get the Notification body.
  67. *
  68. * @return {Object}
  69. ]]
  70. function Notification:getBody()
  71. return self.body
  72. end
  73. --[[
  74. * Set the type of the Notification instance.
  75. *
  76. * @param {Object} type
  77. * @return {void}
  78. ]]
  79. function Notification:setType(type)
  80. self.type = type
  81. end
  82. --[[
  83. * Get the type of the Notification instance.
  84. *
  85. * @return {Object}
  86. ]]
  87. function Notification:getType()
  88. return self.type
  89. end
  90. --[[
  91. * Get a string representation of the Notification instance
  92. *
  93. * @return {string}
  94. ]]
  95. function Notification:toString()
  96. local msg = "Notification Name: " .. self:getName()
  97. msg = msg .. "\nBody: " .. tostring(self:getBody())
  98. msg = msg .. "\nType: " .. self:getType()
  99. return msg
  100. end
  101. return Notification