第三十講:tapestry強大的javaScript混合(mixins)

此例仍是使用上一篇的實例JS,看混合的方法,實在是太強大了!!javascript

JavaScriptMixin.javahtml

/**
* 項目名稱:TapestryStart
* 開發模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 編寫:飛風
* 時間:2012-02-29
*/
package com.tapestry.app.pages;
 
import org.apache.tapestry5.annotations.Property;
 
 
public class JavaScriptMixin {
@Property
@SuppressWarnings("unused")
private String firstName;
 
@Property
@SuppressWarnings("unused")
private String lastName;
}
 

JavaScriptMixin.tmljava

<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" t:mixins="TextboxHint" t:hintText="Enter First Name" t:hintColor="#ff6600"/>
<input t:type="TextField" t:id="lastName" t:mixins="TextboxHint" t:hintText="Enter Last Name" t:hintColor="#808080"/><br/>
</form>
</html>

TextboxHint.javasql

/**
* 項目名稱:TapestryStart
* 開發模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 編寫:飛風
* 時間:2012-02-29
*/
package com.tapestry.app.mixins;
 
import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ClientElement;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.InjectContainer;
import org.apache.tapestry5.annotations.Parameter;
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 TextboxHint {
@Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
private String hintText;
 
@Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
private String hintColor;
 
@Inject
private JavaScriptSupport javaScriptSupport;
 
@InjectContainer
private ClientElement clientElement;
 
public void afterRender() {
 
JSONObject spec = new JSONObject();
spec.put("textboxId", clientElement.getClientId());
spec.put("hintText", hintText);
spec.put("hintColor", hintColor);
javaScriptSupport.addInitializerCall("textboxHint", spec);
}
}
相關文章
相關標籤/搜索