How to populate a Javascript variable in customfield velocity edit template?

Daniel Micallef April 21, 2017

I have passed a List from Java to Velocity templating. Later (in Velocity) I loop through the list of items and create a Javascript object as follows:

<script>
var objectsForSelection = {};
#foreach($carrier in $TableConfigList) #if(${carrier.getHtmlTemplate()}=="") #set($jsonDescriptorWithHtml =${carrier.getJsonTemplateList().toString()}) objectsForSelection['$carrier.getOption()'] ={}; objectsForSelection['$carrier.getOption()']['fields']=JSON.parse('$jsonDescriptorWithHtml'); #end
#end
</script>

This script however is only executed when:

  • The issue is created in a new page, or
  • Create Issue window opens on a screen containing the customfield plugin;

Therefore the question is:

How can I pass data from velocity context to Javascript, and bind the data when the screen containing the Customfield is loaded?

1 answer

1 accepted

0 votes
Answer accepted
Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 21, 2017

Create page elements (hidden or visible depending on the design) that is populated by data in the velocity variables. You can then get values from the page elements in Javascript as you always do.

Daniel Micallef April 24, 2017

@Jobin Kuruvilla thanks for your answer!

It is important to save a JSON object within single quotes, hence avoiding conflicts between begin and end quotes and JSON quotes.

 

Suggest an answer

Log in or Sign up to answer