From fdac3416e19a29d40fa44a54b520ec9e83f2d0f3 Mon Sep 17 00:00:00 2001 From: Sam Georges Date: Mon, 7 Jul 2014 18:39:00 +1000 Subject: [PATCH] Added new layout and page method `onInit()` called after components are initialized and before AJAX requests are processed. --- CHANGELOG.md | 5 ++++- modules/cms/classes/CodeBase.php | 6 ++++++ modules/cms/classes/ComponentBase.php | 4 +++- modules/cms/classes/Controller.php | 17 +++++++++++++++-- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e03c47df8..8363a4154 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +* **Build 11x** (2014-07-xx) + - Added new layout and page method `onInit()` called after components are initialized and before AJAX requests are processed. + * **Build 115** (2014-07-06) - Important! All references to *Email* have been changed to *Mail* and renaming may be required in plugins. - Console command october:update now supports --core, --plugins and --force options. @@ -58,7 +61,7 @@ - Components can now be dragged from the side navigation directly on to the page. - Asset maker methods (addJs, addCss, addRss) now use an optional build code, either *core* or a plugin code. This is converted to a version number to ensure updates are not affected by cached assets. - Added new method `addComponent()` to Cms Controller. Components can now act as a proxy for other components. - - Added new override method `onInit()` to Components, called before AJAX requests are processed. + - Added new override method `init()` to Components, called before AJAX requests are processed. * **Build 90** (2014-05-23) - Class `CmsPropertyHelper` has been deprecated, will be removed year > 2014. diff --git a/modules/cms/classes/CodeBase.php b/modules/cms/classes/CodeBase.php index b11765cad..026fb8f90 100644 --- a/modules/cms/classes/CodeBase.php +++ b/modules/cms/classes/CodeBase.php @@ -41,6 +41,12 @@ class CodeBase extends Extendable implements ArrayAccess parent::__construct(); } + /** + * This event is triggered when all components are initialized and before AJAX is handled. + * The layout's onInit method triggers before the page's onInit method. + */ + public function onInit() {} + /** * This event is triggered in the beginning of the execution cycle. * The layout's onStart method triggers before the page's onStart method. diff --git a/modules/cms/classes/ComponentBase.php b/modules/cms/classes/ComponentBase.php index 7e6d38eed..ec001ce6b 100644 --- a/modules/cms/classes/ComponentBase.php +++ b/modules/cms/classes/ComponentBase.php @@ -17,6 +17,7 @@ abstract class ComponentBase extends Extendable { use \System\Traits\AssetMaker; use \System\Traits\PropertyContainer; + use \October\Rain\Support\Traits\Emitter; /** * @var string A unique identifier for this component. @@ -107,7 +108,8 @@ abstract class ComponentBase extends Extendable /** * Executed when this component is first initialized, before AJAX requests. */ - public function onInit() {} + public function init() {} + public function onInit() {} // Deprecated: Remove ithis line if year >= 2015 /** * Executed when this component is bound to a page or layout, part of diff --git a/modules/cms/classes/Controller.php b/modules/cms/classes/Controller.php index 77164a46b..6cb80756c 100644 --- a/modules/cms/classes/Controller.php +++ b/modules/cms/classes/Controller.php @@ -173,6 +173,18 @@ class Controller extends BaseController $this->initComponents(); + /* + * Give the layout and page an opportunity to participate + * after components are initialized and before AJAX is handled. + */ + CmsException::mask($this->layout, 300); + $this->layoutObj->onInit() + CmsException::unmask(); + + CmsException::mask($this->page, 300); + $this->pageObj->onInit(); + CmsException::unmask(); + /* * Execute AJAX event */ @@ -248,7 +260,7 @@ class Controller extends BaseController protected function initCustomObjects() { $this->layoutObj = null; - + if (!$this->layout->isFallBack()) { CmsException::mask($this->layout, 300); $parser = new CodeParser($this->layout); @@ -307,7 +319,8 @@ class Controller extends BaseController $this->vars[$alias] = $this->page->components[$alias] = $componentObj; } - $componentObj->onInit(); + $componentObj->init(); + $componentObj->onInit(); // Deprecated: Remove ithis line if year >= 2015 return $componentObj; }