代号一个JavaScript回调 [英] Codename one javascript callback

查看:35
本文介绍了代号一个JavaScript回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用中显示同意页面.该页面应使用html编写,因此应用程序需要处理浏览器中的按钮单击事件.我试图复制博客中的示例(减去jQuery部分):

I am trying to display a consent page in my app. The page should be written in html so the app need to handle a button clicked event from browser. I tried to replicate the example in blog (minus the jQuery part):

bc.addJSCallback(
    "document.getElementById('ACCEPT').addEventListener('click', function(){callback.onSuccess(true)})",
    res -> {
        System.out.println(res);
        dialog.dispose();
    }
);

html看起来像这样:

The html looks like this:

<html>
<button id='ACCEPT'>Accept</button>
<button id='DECLINE'>Decline</button>
<script></script>
</html>

单击按钮没有任何反应.但是,当将js表达式放在< script> 标记中时,未记录'callback',这表明js表达式不是原因.

Nothing happened when the button was clicked. However, when the js expression was put in the <script> tag instead, 'callback' is not defined was logged, suggesting that the js expression was not the cause.

我想知道哪一部分出错了?任何帮助表示赞赏.

I'd like to know which part went wrong? Any help is appreciated.

推荐答案

回到最初的问题,似乎可以很容易地解决该问题,并且比简单的 click 事件可以提供更多的数据也被通过.

Coming back to the original question, it seems that the problem can be solved quite easily, and more data than simple click event can be passed as well.

方法1:

使用< form> < a> BrowserComponent 重定向到任何页面,并收听导航事件,例如

Use a <form> or <a> to redirect the BrowserComponent to any page, and listen to navigation event, like

bc.addBrowserNavigationCallback(url -> {
    if (url.equals(initialPath)) { 
        return true; //Show the first page, but do no subsequent redirection
    }
    //Do something here with data encoded in url
});

此链接中所述,可以获得表格数据..

Form data can be obtained as explained in this link.

方法2:

将Javascript对象注册为按钮的 onclick ,或注册为从javascript调用的 function ,如

Register a javascript object either as onclick of a button, or as a function that is called from javascript, as explained in the documentation.

bc.addWebEventListener(BrowserComponent.onLoad, evt -> {
    bc.execute("window.cb = json => {callback.onSuccess(json);};", json -> {
        //Do something with the data here (a json in this case)
    });
});

...和javascript

... and in javascript

const handleFormSubmit = event => {
  event.preventDefault();
  const data = formToJSON(form.elements); //Refer to link above
  const json = JSON.stringify(data);
  //const d = btoa(json); Does not work with unicode character
  cb(json);
};
const form = document.getElementsByName('formmm')[0];
form.addEventListener('submit', handleFormSubmit);


一些事后思考:


A few afterthought:

  1. WebEventListener 不能与 BrowserNavigationCallback
  2. 一起使用
  3. 很难将json作为base64字符串传递,请参见Unicode问题
  4. encodeUrl()是公共的(在 Util 中),而反函数在
  1. WebEventListener cannot be used with BrowserNavigationCallback
  2. Passing json as base64 string is hard, see Unicode Problem
  3. While encodeUrl() is public (in Util), the inverse function is private in BrowserComponent. This complicates passing js object as url
  4. In Method 2 use bc.addJsCallback instead of execute to callback more than once


希望这对某人有帮助


Hope this help someone

这篇关于代号一个JavaScript回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆