第二十九讲:tapestry更好的可重复使用javaScript

源码如下:

RobustJavaScript.java

/**
* 项目名称:TapestryStart
* 开发模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 编写:飞风
* 时间:2012-02-29
*/
package com.tapestry.app.pages;
 
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
 
@Import(library="context:assets/js/robust_textbox_hint.js")
public class RobustJavaScript {
@Property
@SuppressWarnings("unused")
private String firstName;
 
@Property
@SuppressWarnings("unused")
private String lastName;
 
@Property
private String firstNameId;
 
@Property
private String lastNameId;
 
@Inject
private JavaScriptSupport javaScriptSupport;
 
public void setupRender() {
firstNameId = "firstName";
lastNameId = "lastName";
 
JSONObject spec = new JSONObject();
spec.put("textboxId", firstNameId);
spec.put("hintText", "Enter First Name");
spec.put("hintColor", "#ff6600");
javaScriptSupport.addInitializerCall("textboxHint", spec);
 
JSONObject spec2 = new JSONObject();
spec2.put("textboxId", lastNameId);
spec2.put("hintText", "Enter Last Name");
spec2.put("hintColor", "#808080");
javaScriptSupport.addInitializerCall("textboxHint", spec2);
}
}
 

RobustJavaScript.tml

<html t:type="layout" title="tapestryStart Index"  t:sidebarTitle="Framework Version" 
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
<form t:type="form" t:autofocus="false">
<input t:type="TextField" t:id="firstName"/>
<input t:type="TextField" t:id="lastName"/><br/>
</form>
</html>

robust_textbox_hint.js

TextboxHint = Class.create( {
 
initialize : function(textboxId, hintText, hintColor) {
this.textbox = $(textboxId);
this.hintText = hintText;
this.hintColor = hintColor;
this.normalColor = this.textbox.getStyle('color');
 
Event.observe(this.textbox, 'focus', this.doClearHint.bindAsEventListener(this));
Event.observe(this.textbox, 'blur', this.doCheckHint.bindAsEventListener(this));
Event.observe(this.textbox, 'change', this.doCheckHint.bindAsEventListener(this));
Event.observe(this.textbox.form, 'submit', this.doClearHint.bindAsEventListener(this));
 
this.doCheckHint();
},
 
doClearHint : function(e) {
if (this.textbox.value == this.hintText) {
this.textbox.value = "";
}
this.textbox.setStyle({color: this.normalColor});
},
 
doCheckHint : function(e) {
 
// If field is empty, put the hintText in it and set its color to hintColor
 
if (this.textbox.value.length == 0) {
this.textbox.value = this.hintText;
this.textbox.setStyle({color: this.hintColor});
}
 
// Else if field contains hintText, set its color to hintColor
 
else if (this.textbox.value == this.hintText) {
this.textbox.setStyle({color: this.hintColor});
}
 
// Else, set the field's color to its normal color
 
else {
this.textbox.setStyle({color: this.normalColor});
}
 
}
 
} )
 
Tapestry.Initializer.textboxHint = function(spec) {
    new TextboxHint(spec.textboxId, spec.hintText, spec.hintColor);
}

转载于:https://my.oschina.net/shootercn/blog/54366

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值