Dean Hiller
Dean Hiller

Reputation: 20204

playframework, ajax and get request reusability..how?

The tags make me write html that has _ticket while when I am doing the html for the ajax, I write "ticket". Below is the html for the ajax call...(notice the variable ticket)

<div>
    <div class="entry">
        <div class="spacer"></div>
        <span class="label ">Release<span>(Optional)</span></span>
        <span class="input ">
            #{select 'ticket.release', items:releases, valueProperty:'id', labelProperty:'name', value:ticket?.release?.id/}
        </span>
        <div style="clear: both;"></div>
    </div>
</div>

<div>
    <div class="entry">
        <div class="spacer"></div>
        <span class="label ">Subproj/Cat.<span>(Optional)</span></span>
        <span class="input ">
            #{select 'ticket.subproject', items:subprojects, valueProperty:'id', labelProperty:'name', value:ticket?.subproject?.id/}
            <a id="newinfo" class="help" title="&{'help.subproject'}">Help</a>
        </span>
        <div style="clear: both;"></div>
    </div>
</div>

If I want to reuse this page in another page like so

#{projectInfoPage ticket:ticket}

it will not work because I have to modify the above page to be _ticket in every case so it works(I prefer how seam had global variable and the ability to pass in as in this case I would just use a global ticket and be done).

At any rate, is there a way to write some kind of script like this in the playframework so that above page will work in both cases???? oh wait, as a tag, it has to go in the tags folder!!!! grrrr.

hmmm, the only clean way I can think of to solve this is to have that projectInfoPage.html simple be #{projectInfoTag ticket:ticket} and move above code to tags folder and then add #{projectInfoTag ticket:ticket} to my main page for the get request working for both get and for ajax call. This is a bit ugly. Anyone else encounter this? Any way to get rid of the extra file that I have to have to make it re-usable?

Upvotes: 1

Views: 258

Answers (2)

Codemwnci
Codemwnci

Reputation: 54924

The code above can be re-usable, you are just changing ticket to _ticket in too many places. In your select tag, the name element (i.e. the bit that is sent to the server in the POST as the name does not need to change. This is plain text rather than a variable.

So your code should look like the following...

#{select 'ticket.release', items:_releases, valueProperty:'id', labelProperty:'name', value:_ticket?.release?.id/}

This would require you to pass in the releases list and the ticket property. Notice that the ticket.release part at the beginning does not have the underscore prepended. This will mean that the value of the select is sent to the server in the correct way.

Also, if you just want a re-usable bit of HTML code that is not a tag then you can use the include tag.

http://www.playframework.org/documentation/1.2.4/tags#include

As described by the documentation...include includes another template. All of the current template’s variables are directly available in the included template.

<div id="tree">
    #{include 'tree.html' /}
</div>

Upvotes: 2

digiarnie
digiarnie

Reputation: 23415

This is not really elegant and there is probably a better solution, but you could do something like this in your tag:

#{if _ticket}
    #{set ticket:_ticket}
#{/if}

This would set the ticket variable to be the parameter _ticket. Obviously in your situation where ticket is already set and _ticket is not, then the if-statement above would return false and therefore ticket would still work as is.

Upvotes: 1

Related Questions