Bug with Block controller on_page_view?

Permalink
I am on 5.4.0.5.
I wrote a custom block.
I am trying to add some javascripts with bock controller - on_page_view, but they are not getting included.

If I add these block specific scripts in /concrete/elements/page_controls_header.php

then it works. But surely this is not the way to do this.

Any help is much appreciated.

 
mindshines replied on at Permalink Reply
I must add that I want this javascript in Edit mode. is any method I should use?
jbx replied on at Permalink Reply
jbx
The problem is that until you have actually added the block, the blocks controller wont be called. So if you have the block already on the page, then edit it, the javascript should be included, but when you first add the block, it wont be.

The blocks auto.js will always be included, so if you can put your js in there, that will work. Alternatively, you could put your <script src=... line into your add.php file if that isn't too late in the chain.

Oh, and here's another option that's a bit different. I had a very similar problem a few weeks back, but I wanted to include a css file for the interface of the add.php. Currently, this css has to be in your main css file, which means it's included all the time - or it ends up being put directly into add.php which is invalid code. So my solution was this:
var filename = '/packages/jbx_zend_form/blocks/jbx_zend_form/assets/css/interface.css';
var fileref  = document.createElement("link");
$(fileref).attr('rel', 'stylesheet');
$(fileref).attr('type', 'text/css');
$(fileref).attr('href', filename);
$('head').append($(fileref));


Hope something in there somewhere helps :)

Jon
mindshines replied on at Permalink Reply
Thank you Jon for these leads.
I need to try one by one this weekend.
I will inform the results.
Hope something works out :)

Thanks again.
Shotster replied on at Permalink Reply
Shotster
> The problem is that until you have actually added
> the block, the blocks controller wont be called.

That's not true. The controller is loaded when adding and editing.

-Steve
Shotster replied on at Permalink Reply
Shotster
> I must add that I want this javascript in Edit mode.

Assuming you're using the addHeaderItem() method, put the code that loads the JS in a separate method of your controller and then call that method from both the add() and edit() methods of the controller.

Calling that method from on_page_view() should also load the JS during a normal page view. The auto.js file is for JS code that's needed ONLY when adding/editing a block but not during a normal page view. (IMO, for clarity, that file should be named addedit.js.)

-Steve
mindshines replied on at Permalink Reply
Thanks Steve.
I need the javascripts on for add/edit. So I will try with auto.js.

But I want to create a block similar to
http://www.gamabhana.com/gamabhana_pro/...
for indian language, but the order of javascript is important.

will keep you posted.
Shotster replied on at Permalink Reply
Shotster
andrew replied on at Permalink Reply
andrew
Don't use auto.js – use $this->addHeaderItem() from within the edit() and add() functions. Then those will be added to the page dynamically when the block is in add and edit mode.
Shotster replied on at Permalink Reply
Shotster
Just curious, Andrew...

Is there any situation where one would actually NEED to use auto.js? I never use it, as I've found that the method you describe works fine and is more consistent with the way header items are added in other situations.

-Steve
andrew replied on at Permalink Reply
andrew
not really. It was a stopgap solution before we had addHeaderItem() working with add() and edit() (we would always just include auto.js whether it was there or not.) But the add() and edit() approach (once we added it later) was much more elegant.