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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Vanja D.
  • 834
  • 13
  • 20

1 Answers1

0

I solved this by modifying the controller->helpers['helperName'] array from the Component.

Since the Helper is instantiated after the component's beforeRender call, I can modify the settings for the helper and access the data from the Helper::__contruct($View, $settings) call.

I'm not sure if I can rely on CakePHP's internals to keep this order of callbacks, but it works so far.

Vanja D.
  • 834
  • 13
  • 20
  • Simply add the helper into the array when you want to autoload. Also, there is already such a thing (albeit without a component): https://github.com/predominant/goodies – dr Hannibal Lecter Jun 09 '12 at 22:20