You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.2 KiB

  1. local Component = class("Component")
  2. function Component:ctor(name)
  3. self.name_ = name
  4. end
  5. function Component:getName()
  6. return self.name_
  7. end
  8. function Component:getTarget()
  9. return self.target_
  10. end
  11. function Component:exportMethods_(methods)
  12. self.exportedMethods_ = methods
  13. local target = self.target_
  14. local name = self.name_
  15. local com = self
  16. for _, key in ipairs(methods) do
  17. if not target[key] then
  18. target[key] = function(__, ...)
  19. local r = {com[key](self, ...)}
  20. if r[1] == self then r[1] = target end
  21. return unpack(r)
  22. end
  23. end
  24. end
  25. return self
  26. end
  27. function Component:bind_(target)
  28. if self.target_ then
  29. return;
  30. end
  31. self.target_ = target
  32. self:onActivate(target)
  33. end
  34. function Component:unbind_()
  35. if self.target_ == nil then
  36. return;
  37. end
  38. if self.exportedMethods_ then
  39. local target = self.target_
  40. for _, key in ipairs(self.exportedMethods_) do
  41. target[key] = nil
  42. end
  43. end
  44. self:onDeactivate()
  45. self.target_ = nil;
  46. end
  47. function Component:onActivate()
  48. end
  49. function Component:onDeactivate()
  50. end
  51. return Component