MacroCommand.lua 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. --[[
  2. * @author PureMVC Lua Native Port Sean
  3. * @author Copyright(c) 2006-2012 Futurescale, Inc., Some rights reserved.
  4. *
  5. * @class puremvc.MacroCommand
  6. * @extends puremvc.Notifier
  7. *
  8. * A base command implementation that executes other commands, such as
  9. * {@link puremvc.SimpleCommand SimpleCommand}
  10. * or {@link puremvc.MacroCommand MacroCommand}
  11. * subclasses.
  12. *
  13. * A MacroCommand maintains an list of
  14. * command constructor references called *SubCommands*.
  15. *
  16. * When #execute is called, the MacroCommand
  17. * instantiates and calls #execute on each of its *SubCommands* in turn.
  18. * Each *SubCommand* will be passed a reference to the original
  19. * {@link puremvc.Notification Notification}
  20. * that was passed to the MacroCommands #execute method
  21. *
  22. * Unlike {@link puremvc.SimpleCommand SimpleCommand},
  23. * your subclass should not override #execute but instead, should
  24. * override the #initializeMacroCommand method, calling #addSubCommand once for
  25. * each *SubCommand* to be executed.
  26. *
  27. * If your subclass does define a constructor, be sure to call "super" like so
  28. *
  29. * function MyMacroCommand ()
  30. * {
  31. * MacroCommand.call(this);
  32. * };
  33. * @constructor
  34. ]]
  35. local Notifier = require( framework.PACKAGE_NAME .. '/patterns/observer/Notifier')
  36. local MacroCommand = class('MacroCommand', Notifier)
  37. function MacroCommand:ctor()
  38. MacroCommand.super.ctor(self)
  39. self.subCommands= {}
  40. self:initializeMacroCommand()
  41. end
  42. --[[
  43. * @protected
  44. * Initialize the MacroCommand.
  45. *
  46. * In your subclass, override this method to
  47. * initialize the MacroCommand's *SubCommand*
  48. * list with command class references like
  49. * this:
  50. *
  51. * // Initialize MyMacroCommand
  52. * MyMacroCommand.prototype.initializeMacroCommand= function ()
  53. * {
  54. * this.addSubCommand( com.me.myapp.controller.FirstCommand );
  55. * this.addSubCommand( com.me.myapp.controller.SecondCommand );
  56. * this.addSubCommand( com.me.myapp.controller.ThirdCommand );
  57. * };
  58. *
  59. * Note that *SubCommand*s may be any command implementor,
  60. * MacroCommands or SimpleCommands are both acceptable.
  61. * @return {void}
  62. ]]
  63. function MacroCommand:initializeMacroCommand() end
  64. --[[
  65. * @protected
  66. * Add a *SubCommand*
  67. *
  68. * The *SubCommand*s will be called in First In / First Out (FIFO) order
  69. * @param {Function} commandClassRef
  70. * A reference to a subclassed SimpleCommand or MacroCommand constructor
  71. ]]
  72. function MacroCommand:addSubCommand(commandClassRef)
  73. table.insert(self.subCommands, commandClassRef)
  74. end
  75. --[[
  76. * Execute this MacroCommands *SubCommands*
  77. *
  78. * The *SubCommand*s will be called in First In / First Out (FIFO) order
  79. * @param {puremvc.Notification} note
  80. * The Notification object to be passed to each *SubCommand*
  81. ]]
  82. function MacroCommand:execute(note)
  83. -- SIC- TODO optimize
  84. while(#self.subCommands > 0) do
  85. local ref= table.remove(self.subCommands, 1)
  86. local cmd= ref.new()
  87. cmd:initializeNotifier(self.multitonKey)
  88. cmd:execute(note)
  89. end
  90. end
  91. return MacroCommand