Block Add and block ID - chicken and egg problem?

Permalink
I am developing a block where an option on the Add/Edit form pops up a dialog that is handled by ajax. The ajax handler is an action in the block controller (ie. not a separate tool).

It all works fine when in Edit, but when adding a new block every method I have tried for creating the link to the ajax actions fails. My current hypothesis is that all these methods at some point use the block ID, and that during the Add process the block ID does not yet exist (during Edit it does exist, so any of the various methods of creating the action link work fine). Can anyone confirm my assumption about the block ID?

I had a look at other blocks that use a pop up and ajax from the Add/Edit dialog (like autonav and several of the image galleries). Those I have examined use external tools rather than using an action within the controller.

Am I missing something obvious? Has anyone else solved this (other than by making the action an external tool)?

JohntheFish
 
Shotster replied on at Permalink Reply
Shotster
Is there a reason you have an aversion to using a tool? Generally speaking, it's the proper C5 way to go about it.

-Steve
JohntheFish replied on at Permalink Reply
JohntheFish
The actions are small and they use other functions in the controller, so I decided to keep them with it rather than split them out to tasks. To my mind with such small actions and inter-dependency, keeping it all together will be easier to maintain. As fore the C5 way, both ways are well documented, so I started from the assumption that both would be equally valid.
Shotster replied on at Permalink Reply
Shotster
Perhaps sharing some code or providing more detail about what you're trying to accomplish with the AJAX request would be helpful. Why it would work for editing and not adding is unclear to me at this point.

-Steve
JohntheFish replied on at Permalink Reply
JohntheFish
The actual problem block is too big. I will try and boil it down to a trivial case that shows the issue and post it here, probably tomorrow.
Mnkras replied on at Permalink Best Answer Reply
Mnkras
$this->action() in blocks uses the bID,

also you can move that code to a tools file and use this in it, Loader::block('autonav'); replacing the handle, and that will load the blocks controller,
jordanlev replied on at Permalink Reply
jordanlev
I agree that it would be conceptually easier to keep everything together in the controller, but the fact that you're running into problems doing it that way negates your assumption that both would be equally valid.
I would just make the tools file and use it as a thin wrapper for the controller (assuming this is even possible at all -- which it might not be because a bID doesn't exist until the block has been added). Mnkras's code should do the trick. If not, seeing the code would be helpful (and also if you could explain what it is you're trying to do that would help as well -- perhaps we could come up with alternative solutions to the problem).
JohntheFish replied on at Permalink Reply 1 Attachment
JohntheFish
I have boiled a set of ajax examples down to a minimalistic amount of code, attached as a package jl_test_section. What I would ideally like to achieve is a way to get test_1 in the block to work for add (ie. before bID exists, so not using $this->action()).
However, while putting the package together I have come to accept that using an external tools file, as you all suggest, may be the only way I can have ajax within an add dialog.
Thank you all for your advice.
On a related note, having put this example package together, should I submit it to the marketplace (free of course) as it may be of use to others making their first forays into ajax for C5? What changes would you suggest in the package to make it suitable as such a learning tool?
jordanlev replied on at Permalink Reply
jordanlev
I appreciate that you want to share with others to help them learn. But I think a better venue than the marketplace would be writing a how-to for the documentation.

If you really wanted to put something in the marketplace though I would suggest that an example of a block or package that actually does something would be more helpful -- because using ajax in a block add/edit dialog is not a "normal" thing to do, I think that people would need to see an example of how it's actually used in context in order to make sense of it.

-Jordan
JohntheFish replied on at Permalink Reply
JohntheFish
OK, I take your point about a marketplace package needing to actually do something and that the test is too abstract. I had a look at the how-to submission form will probably need some guidance, but its probably best if I think about it more and then maybe start another thread.
JohntheFish replied on at Permalink Reply
JohntheFish
In the end I tidied it up and polished it into a marketplace free add-on. "Ajax Lessons"http://www.concrete5.org/marketplace/addons/ajax-lessons/... .
I thought about changing it to a how-to, but that would have lost the interactive element and mostly been a synopsis of other documentation. I wanted the interactive side of it to work as a lesson, hence the add-on.