帮忙在 Tapestry5 中创建 JavaScript mixin?

发布于 09-01 03:52 字数 1883 浏览 10 评论 0原文

我正在创建一个 mixin,当文本字段获得焦点时它会呈现一个 javascript 文件。

我对 Tapestry 中 mixins 的想法很陌生,我不确定将我的原始 javascript 文件放置在哪里,我希望在文本字段获得焦点时运行该文件。

以下是我的代码示例: Java mixin 类:

package asc.mixins;

import org.apache.tapestry5.RenderSupport;
import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
import org.apache.tapestry5.annotations.InjectContainer;
import org.apache.tapestry5.corelib.base.AbstractTextField;

@IncludeJavaScriptLibrary("js_dasher_mixin.js")
public class DasherMixin {

@Environmental
private RenderSupport renderSupport;

@InjectContainer
private AbstractTextField field;

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


}

Javascript mixin 文件:

JSDasher = Class.create({

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

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

onFocus: function(event)
{
    //call my javascript init() function
} 
}

我的 javascript 文件的一部分 我希望在文本字段获得焦点时运行:

var posX, posY;


// Sets up our global variables and dispatches an init request to the server.
function init() {

posX=0;
posY=0;
canvas = document.getElementById("canvas"); 
canvasWidth = canvas.offsetWidth;
canvasHeight = canvas.offsetHeight;
if (canvas.getContext) {   
    ctx = canvas.getContext("2d");
}

canvas.onclick = canvasClicked;
canvas.onmousemove = mouseMoved; 

canvasOffsetX = findPosX(canvas);
canvasOffsetY = findPosY(canvas);

sessID = -1;

sendInitRQ(canvasWidth, canvasHeight);

}

我的 javascript 文件比上面大得多,我的问题是我应该把我的 javascript 代码放在上面的哪里?

是否应该全部包含在 mixin.js 文件中?如果是的话,它到底应该去哪里?

预先感谢您的任何帮助。

I am creating a mixin which renders a javascript file when a textfield gains focus.

I am new to the idea of mixins in Tapestry, and I am unsure of where to place my original javascript file which i wish to run when the textfield gains focus.

The following is an example of my code:
The Java mixin class:

package asc.mixins;

import org.apache.tapestry5.RenderSupport;
import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
import org.apache.tapestry5.annotations.InjectContainer;
import org.apache.tapestry5.corelib.base.AbstractTextField;

@IncludeJavaScriptLibrary("js_dasher_mixin.js")
public class DasherMixin {

@Environmental
private RenderSupport renderSupport;

@InjectContainer
private AbstractTextField field;

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


}

The Javascript mixin file:

JSDasher = Class.create({

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

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

onFocus: function(event)
{
    //call my javascript init() function
} 
}

part of my javascript file I wish to run when the textfield gains focus:

var posX, posY;


// Sets up our global variables and dispatches an init request to the server.
function init() {

posX=0;
posY=0;
canvas = document.getElementById("canvas"); 
canvasWidth = canvas.offsetWidth;
canvasHeight = canvas.offsetHeight;
if (canvas.getContext) {   
    ctx = canvas.getContext("2d");
}

canvas.onclick = canvasClicked;
canvas.onmousemove = mouseMoved; 

canvasOffsetX = findPosX(canvas);
canvasOffsetY = findPosY(canvas);

sessID = -1;

sendInitRQ(canvasWidth, canvasHeight);

}

My javascript file is much larger than above, my question is where should I put my javascript code above?

Should it all be contained in the mixin.js file? if so where exactly should it go?

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在看孤独的风景2024-09-08 03:52:11

该方法在 mixin.js 中可以自由浮动,但使用像 init 这样的名称,可能会发生冲突。您可以将其放在 JSDasher 类本身中或将主体移至 onFocus 函数。您还可以在 JSDasher 类上定义任何其他函数并使用 this.function_name 调用它们。以 Tapestry 源代码中的 datefield.js 为例。

The method is ok free-floating in mixin.js but with name like init, you might have a conflict. You can put it in the JSDasher class itself or move the body to the onFocus function. You can also define any other functions on the JSDasher class and call them with this.function_name. Look at datefield.js in the Tapestry source as an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文