Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to pass data from JS file to Velocity template?

thuytiendang206
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 18, 2018

I want to get data from rest API via javascript and show it in velocity temple.

In file example.js

AJS.toInit(function(){
var user = getCurrentUserName();
AJS.$(".....)....
...
}
function getCurrentUserName()
{
var baseUrl = AJS.params.baseURL;
var user;
AJS.$.ajax({
url: baseUrl+'/rest/gadget/1.0/currentUser',
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
user = data
}
});
return user;
}

 In file example.vm

<div class="field-group">
<label for="multiselect">Multi select</label>
<select class="multi-select" size="4" multiple="multiple" id="multiselect" name="multiselect">
#foreach ($user in $user)
<option>$user.username</option>
#end
</select>
</div>

I want to get the user variable as $user in velocity template.

Could I do that?

 

1 answer

0 votes
Danyal Iqbal
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.
July 18, 2018

No, Velocity is a fassad for java objects not for javascript objects.  try

  $webResourceManager.requireResource("com.atlassian.auiplugin:ajs")
    $webResourceManager.requireResource("com.xxx.example:example-resources")

in your example.vm body  and add the resource in atlassian-plugin.xml. handle the foreach loop in the js.

thuytiendang206
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 20, 2018

yes, thank you 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events