开发者

How to access playframework's controller data in view javascript?

开发者 https://www.devze.com 2023-02-24 20:30 出处:网络
I have a javascript application where I\'d like to add 开发者_运维问答autocompletion to a form. Therefore, I render an array containing all available names in the controller so the array is available

I have a javascript application where I'd like to add 开发者_运维问答autocompletion to a form. Therefore, I render an array containing all available names in the controller so the array is available in the view (html).

$(function() {${"id"}.autocomplete({source: how to get the data here??}); });

Now I'd like read this data into a variable so I can use it in my javascript for the autocompletion... Can anyone tell me how to achieve this?

TIA

- astriffe


You need this data to be serialized in JSON, that when printed in javascript will be interpreted as javascript.

To help you with that, play! has Google JSON library

An example how could you achieve this is:

<% def gson = new com.google.gson.Gson(); %>
$(function() {${"id"}.autocomplete({source: ${gson.toJson(playVariable)} }); });

But this is ugly code, it would be better to use template extensions to achieve this

0

精彩评论

暂无评论...
验证码 换一张
取 消