Monday, March 5, 2012

Drop-down Form Submissions in Trinidad

I've recently been doing this the very wrong way.  So I'll be going over the wrong way, and the right way.

The embarrassingly wrong way, is to have an Apache Trinidad drop down followed by a command link.  You set the onchange of the drop down to document.getElementById('commandLinkId').click() and have the command link do the submission.  I swear I found some webpage that recommended this, but I can't find it anymore.  As a side note, if you're doing things REALLY WRONG you can put the value of the dropdown as a variable instead of a property and try to read it in the actionListener of the command link.  This method may truly be Daily WTF worthy.

The less wrong way (using Trinidad), but the seemingly standard way to do it in normal JSF to do it would be use onclick="this.form.submit()".  This is a decent way for normal (non-Trinidad) JSF, but Trinidad makes it even easier!

The RIGHT way is to use Trinidad's autoSubmit feature.  This not only fixed a bug (doing it the VERY WRONG WAY resulted in a race condition with the version I was using), but saved lines of gristly code, avoids JavaScript (being used directly) and cuts out an invisible commandLink.

Please, please avoid my foolishness.

No comments:

Post a Comment