简体   繁体   English

Vaadin和独立的JavaScript事件

[英]Vaadin and independent JavaScript Events

I develop strength password indicator using ProgressBar and PasswordField. 我使用ProgressBar和PasswordField开发强度密码指示器。 The problem is that I can't use jQuery events handlers like eg onkeydown or native javascript's addEventListener because It works only one time then Vaadin somehow override it. 问题是我无法使用onkeydown或本机javascript的addEventListener之类的jQuery事件处理程序,因为它只能运行一次,然后Vaadin会以某种方式覆盖它。 I dont want to remove it permanently and completly, it should work only for jQuery object. 我不想永久性地完全删除它,它仅适用于jQuery对象。 Any ideas or better solution will be welcome. 任何想法或更好的解决方案都将受到欢迎。

Any ideas or better solution will be welcome. 任何想法或更好的解决方案都将受到欢迎。

That's how batman would do it (code first, explanation later): 蝙蝠侠就是这样做的(先编码,后解释):

@SuppressWarnings("serial")
@Theme("so5")
@JavaScript("batman.js")
public class So5UI extends UI {

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = So5UI.class)
    public static class Servlet extends VaadinServlet {
    }

    @Override
    protected void init(VaadinRequest request) {
        VerticalLayout layout = new VerticalLayout();
        TextField field = new TextField();
        field.setId("batmanField");
        layout.addComponent(field);
        setContent(layout);
    }
}

And batman.js located in the same package as So5UI class. batman.js与So5UI类位于同一软件包中。

alert('Script loaded');
var i = setInterval(batmanFunction, 300);

function batmanFunction()
{
    var element = document.getElementById("batmanField");
    if(element != null)
    {
        element.addEventListener("mouseover", batmanScript);
        clearInterval(i);
    }
}
function batmanScript()
{
    alert('I am batman');
}

This code works and every time you mouse over your textfield an alert with "I am batman" text appears. 此代码有效,并且每次将鼠标悬停在文本字段上时,都会出现带有“我是蝙蝠侠”文本的警报。 I would like to point out some most important stuff: 我想指出一些最重要的内容:

  1. I've added an Id to the field so we can find it by getElementById() 我已经向该字段添加了一个Id ,以便我们可以通过getElementById()找到它
  2. I run batmanFunction which register a listener to the field in intervals. 我运行batmanFunction ,它定期间隔向该字段注册一个侦听器。 That's because we have no control over execution time of JavaScript and DOM creation. 这是因为我们无法控制JavaScript和DOM创建的执行时间。 We need to register listener after DOM creation. 创建DOM 之后,我们需要注册侦听器。
  3. While working with JavaScript and Vaadin be sure to disable js caching in your browser because sometimes it leads to confusion ("This script should work" while browser still use cached script). 在使用JavaScript和Vaadin时,请确保在浏览器中禁用js缓存,因为有时这会引起混淆(在浏览器仍使用缓存脚本的情况下,“此脚本应该可以工作”)。 Here is how to do it on Firefox. 是在Firefox上的操作方法。

The code has been tested on Firefox 34, Vaadin 7.3, Tomcat 7.0. 该代码已在Firefox 34,Vaadin 7.3,Tomcat 7.0上进行了测试。 If you still having troubles please provide more details. 如果您仍然遇到麻烦,请提供更多详细信息。

EDIT: tested and works for keydown too 编辑:经测试,也适用于keydown

EDIT2 I believe I figured out why it might only works "once" for you. EDIT2我相信我已经弄清楚了为什么它可能仅对您“起作用”一次。 Since Vaadin removes and adds div's automatically via Ajax, you must add event listener to the component every time it's added to the DOM. 由于Vaadin会通过Ajax自动删除和添加div,因此每次将事件侦听器添加到DOM时,都必须将事件侦听器添加到组件。 In example: if you click on button which removes the field from layout, and later you will click on button which adds the same field to the layout the event will no longer be generated. 例如:如果单击按钮将其从布局中删除,然后再单击将相同的字段添加到布局中的按钮,则将不再生成事件。 You must find those places and execute given script once again. 您必须找到这些位置并再次执行给定脚本。 You can check that with following code: 您可以使用以下代码进行检查:

    @Override
    protected void init(VaadinRequest request) {
    VerticalLayout layout = new VerticalLayout();
    TextField field = new TextField();
    field.setId("batmanField");
    layout.addComponent(field);
    Button button = new Button("remove");
    button.addClickListener(new ClickListener()
    {

        @Override
        public void buttonClick(ClickEvent event)
        {
            layout.removeComponent(field);
        }
    });
    layout.addComponent(button);
    Button button2 = new Button("add");
    button2.addClickListener(new ClickListener()
    {

        @Override
        public void buttonClick(ClickEvent event)
        {
            layout.addComponent(field);
            com.vaadin.ui.JavaScript.eval("setInterval(batmanFunction, 300)");
        }
    });
    layout.addComponent(button2);
    setContent(layout);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM