1

I am writing a component+helper pair which handle inclusion of JS files for the current View.

The idea is to autoload JS files which have the same name as the view (or action). So if you have a view called 'admin_add.ctp' and a file 'admin_add.js' in the same folder, the JS file would be included in the JS buffer. I would prefer this to happen automatically in both beforeRender calls.

The tricky part is that I need to be able to enable/disable the autoload at the Controller::action level (with a component), but the inclusion logic needs to happen at the View level (with a Helper).

How do I pass the enable/disable trigger from the Component to the Helper, without having to write code in each view template?

4

1 回答 1

0

我通过修改组件中的 controller->helpers['helperName'] 数组解决了这个问题。

由于 Helper 是在组件的 beforeRender 调用之后实例化的,因此我可以修改 helper 的设置并从 Helper::__contruct($View, $settings) 调用访问数据。

我不确定我是否可以依靠 CakePHP 的内部来保持这种回调顺序,但到目前为止它仍然有效。

于 2012-06-09T18:08:09.920 回答