简体   繁体   English

如何在Tapestry5中创建一个自定义文本字段,将一些Javascript呈现到页面上?

[英]How do I create a custom text field in Tapestry5 that renders some Javascript onto the page?

I have been trying to create a custom textfield in tapestry which will render some javascript when it gains focus. 我一直在尝试在tapestry中创建一个自定义文本字段,当它获得焦点时将呈现一些javascript。 But I have been having trouble trying to find an example of this. 但我一直在努力寻找一个这样的例子。

Here is some of the code i have started off with: 以下是我开始使用的一些代码:

package asc.components;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.Field;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.ComponentDefaultProvider;


public class DahserTextField implements Field {

@Parameter (defaultPrefix = "literal")
private String label;
@Inject
private ComponentResources resources;
@Inject
private ComponentDefaultProvider defaultProvider;
@Parameter
private boolean disabled;
@Parameter
private boolean required;

String defaultLabel(){
    return defaultProvider.defaultLabel(resources);
}

public String getControlName() {
    return null;
}

public String getLabel() {
    return label;
}

public boolean isDisabled() {
    return disabled;
}

public boolean isRequired() {
    return required;
}

public String getClientId() {
    return resources.getId();
}


}

I have been unsure on what to do next. 我不确定下一步该做什么。 I do not know what to put into the .tml file. 我不知道该把什么放到.tml文件中。 I would be grateful if anyone could help or point me in the right direction. 如果有人能帮助或指出我正确的方向,我将不胜感激。

There is no need to replicate any of TextField 's functionality in your own component, instead you should create a component mixin. 无需在您自己的组件中复制任何TextField的功能,而是应该创建组件mixin。 Mixins are designed to add behaviour to existing components. Mixins旨在为现有组件添加行为。

From the Tapestry 5 docs : 来自Tapestry 5文档

Tapestry 5 includes a radical feature, component mixins. Tapestry 5包含一个基本功能,即组件混合。 Component mixins are a tricky concept; 组件混合是一个棘手的概念; it basically allows a true component to be mixed together with special limited components called mixins. 它基本上允许真正的组件与称为mixins的特殊有限组件混合在一起。 The component plus its mixins are represented as just a single tag in the component template, but all the behavior of all the elements. 组件及其mixins在组件模板中仅表示为单个标记,但表示所有元素的所有行为。

You would use the mixin like this: 你会像这样使用mixin:

<input type="text" t:type="TextField" t:mixins="MyMixin" t:someParam="foo" />

A mixin stub: mixin存根:

@IncludeJavaScriptLibrary("MyMixin.js")
public class MyMixin {

    /**
     * Some string param.
     */
    @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
    private String someParam;

    @Environmental
    private RenderSupport renderSupport;

    @InjectContainer
    private AbstractTextField field;

    @AfterRender
    void addScript() {
        this.renderSupport.addScript("new MyJavascriptClass('%s', '%s');", 
                this.field.getClientId(), this.someParam);
    }

}

Note the @InjectContainer annotation, which injects the containing TextField into your Mixin. 注意@InjectContainer注释,它将包含TextField注入到Mixin中。 In this case, we want the TextField's clientId. 在这种情况下,我们需要TextField的clientId。

Also note the @IncludeJavaScriptLibrary("MyMixin.js") annotation, which includes the required Javascript file. 另请注意@IncludeJavaScriptLibrary("MyMixin.js")注释,其中包含所需的Javascript文件。

The Javascript could look like this: Javascript可能如下所示:

MyJavascriptClass = Class.create({

    initialize: function(textField, someParam) 
    {
        this.textField = $(textField);
        this.someParam = someParam;

        this.textField.observe('focus', this.onFocus.bindAsEventListener(this));
    },

    onFocus: function(event)
    {
        //do something
    }
}

The key difference to your approach is that this involves defining your own JS class and using Tapestry's built-in facilities to load and initialize the JS. 您的方法的主要区别在于,这涉及定义您自己的JS类并使用Tapestry的内置工具来加载和初始化JS。 The use of mixins is also relatively light-weight and elegant in comparison to creating your own components. 与创建自己的组件相比,mixins的使用也相对轻巧和优雅。

The .tml .tml

<t:textfield onfocus="somethingCool()" />

The Java should probably extent TextField? Java可能应该扩展TextField? It will need to import a new stylesheet too probably. 它可能需要导入一个新的样式表。

-- Pages are actually components, so you would build a component just like you would have any other page. - 页面实际上是组件,因此您将构建一个组件,就像您拥有任何其他页面一样。 You can embed any other component into them. 您可以将任何其他组件嵌入其中。 I hope this is a good starting point for you. 我希望这对你来说是一个很好的起点。

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

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