如何将参数传递给scala中的javascript元素?

时间:2022-07-06 07:22:54

I need to feed a javascript element with values from a Map as shown below.

我需要使用Map中的值来提供javascript元素,如下所示。

My render feeds the HTML document with:

我的渲染为HTML文档提供:

@(morrisDonut: Map[String, Integer])

The JS looks like this:

JS看起来像这样:

$.getScript('https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.2/raphael-min.js',function(){
$.getScript('https://cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js',function(){

var donutData = [];
@for((activity, steps) <- morrisDonut) {
    donutData.push({label: activity, value: steps});
}

Morris.Donut({
    element: 'user_activity-donut',
    data: donutData
});
});
});

The script stops because it dont can't find/can't use activity and steps even though its the correct types.

该脚本停止,因为即使它的类型正确,它也找不到/不能使用活动和步骤。

SOLUTION:

donutData.push({label: '@activity', value: @steps});

Adding " ' " and " @ " symbols seemed to work just fine

添加“'”和“@”符号似乎工作正常

1 个解决方案

#1


From your syntax I think (and I'm inferring a lot here) that you're using the Play Framework - i.e. Twirl templates. If that is the case, then you're just missing some deference operators (@) at the critical points where you are loading up your donutData array:

从你的语法我认为(我在这里推断很多)你正在使用Play框架 - 即Twirl模板。如果是这种情况,那么你只是在加载donutData数组的关键点缺少一些deference运算符(@):

@for((activity, steps) <- morrisDonut) {
  donutData.push({label: @activity, value: @steps});
}

It can be tricky intermixing Twirl directives with another language because by design, there's no explicit "this is the end of my Twirl code" marker - it's all inferred. So in your case, the first and third lines were correctly interpreted by Twirl, but the second line appeared to just be some static content - which would be inserted as-is.

将Twirl指令与另一种语言混杂起来可能很棘手,因为根据设计,没有明确的“这是我的Twirl代码的结尾”标记 - 这都是推断的。所以在你的情况下,第一行和第三行被Twirl正确解释,但第二行看起来只是一些静态内容 - 它将按原样插入。

By adding the magic @ symbols, the necessary substitution is detected.

通过添加magic @符号,可以检测到必要的替换。

#1


From your syntax I think (and I'm inferring a lot here) that you're using the Play Framework - i.e. Twirl templates. If that is the case, then you're just missing some deference operators (@) at the critical points where you are loading up your donutData array:

从你的语法我认为(我在这里推断很多)你正在使用Play框架 - 即Twirl模板。如果是这种情况,那么你只是在加载donutData数组的关键点缺少一些deference运算符(@):

@for((activity, steps) <- morrisDonut) {
  donutData.push({label: @activity, value: @steps});
}

It can be tricky intermixing Twirl directives with another language because by design, there's no explicit "this is the end of my Twirl code" marker - it's all inferred. So in your case, the first and third lines were correctly interpreted by Twirl, but the second line appeared to just be some static content - which would be inserted as-is.

将Twirl指令与另一种语言混杂起来可能很棘手,因为根据设计,没有明确的“这是我的Twirl代码的结尾”标记 - 这都是推断的。所以在你的情况下,第一行和第三行被Twirl正确解释,但第二行看起来只是一些静态内容 - 它将按原样插入。

By adding the magic @ symbols, the necessary substitution is detected.

通过添加magic @符号,可以检测到必要的替换。