小门6:如何通过AbstractDefaultAjaxBehavior注入javascript函数? [英] Wicket 6: Howto inject a javascript function via AbstractDefaultAjaxBehavior?
问题描述
我有一个页面,页面上附加了一个JavaScript文件.
I have a page that has a JavaScript file attached to it.
JavaScript具有一个函数,最后应调用Wicket通过AbstractDefaultAjaxBehavior注入的函数.
The JavaScript has a function that, at the end should call a function that has been injected by Wicket through AbstractDefaultAjaxBehavior.
JavaScript如下所示:
The JavaScript looks like this:
function updateAmount(amount){
// do some calculations on amount
saveAmount(amount);
}
注入的函数应如下所示:
The injected function should look something like this:
function saveAmount(amount){
Wicket.Ajax.post({
u: '${callbackUrl}',
dep: function(){
return [{name:'amount','value':amount}];
}
}
我遇到的问题是,当updateAmount
调用saveAmount
函数时,(控制台)日志指出"saveAmount"未定义.
The problem I have is that when updateAmount
calls the saveAmount
function the (console) log states that the "saveAmount" is undefined.
如果我查看源代码,注入的函数在那里,但不是以wicket生成的JavaScript而是"AJAX" JavaScript的形式出现.
If I look at the source, the injected function is there but not as JavaScript but as "AJAX" JavaScript generated by wicket.
目标是在JavaScript中调用一个函数,该函数将调用Wicket注入的函数,该函数使用JavaScript提供的参数执行AJAX调用.
The goal is to call a function in JavaScript that will call a function injected by Wicket that performs an AJAX call with parameters that are provided by JavaScript.
我将非常感谢您的帮助.
I would really appreciate any help.
推荐答案
基本上,您应该在自定义AbstractAjaxBehaviour
Basically, you should use the updateAjaxAttributes()
and renderHead()
in a custom AbstractAjaxBehaviour
在此处查看更多详细信息:
See for more detail here:
http://wickedsource.org/2013/01/07/rolling-your-own-ajax-behavior-with-wicket/
http://wickedsource.org/2013/01/07/rolling-your-own-ajax-behavior-with-wicket/
从Google缓存中检索相关代码(由Tom Hombergs编写):
Retrieved from google cache, relevant code (written by Tom Hombergs):
public class MyAjaxBehavior extends AbstractDefaultAjaxBehavior {
@Override
protected void respond(AjaxRequestTarget target) {
RequestCycle cycle = RequestCycle.get();
WebRequest webRequest = (WebRequest) cycle.getRequest();
StringValue param1 = webRequest.getQueryParameters().getParameterValue("param1");
StringValue param2 = webRequest.getQueryParameters().getParameterValue("param2");
// do whatever you need with param1 and param2
}
@Override
public CharSequence getCallbackScript() {
String script = super.getCallbackScript().toString();
script = script.replace("\"PLACEHOLDER1\"", "param1Value");
script = script.replace("\"PLACEHOLDER2\"", "param2Value");
return script;
}
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
attributes.getExtraParameters().put("param1", "PLACEHOLDER1");
attributes.getExtraParameters().put("param2", "PLACEHOLDER2");
}
@Override
public void renderHead(Component component, IHeaderResponse response) {
super.renderHead(component, response);
String script = "var param1Value = My.JavaScript.Module.calculate1();";
script += "var param2Value = My.JavaScript.Module.calculate2();";
script += getCallbackScript();
response.render(OnDomReadyHeaderItem.forScript(script));
}
}
这篇关于小门6:如何通过AbstractDefaultAjaxBehavior注入javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!