Hi,
I have a new requirement that came up and I've been considering how to
handle accordingly. The issue at hand is to use an AJAX mixin to read a
change to a Select input, update the value bound to a TextField, and
conditionally set the "disabled" attribute of the same TextField.
I implemented something a little less complicated for a similar
interface. This other page uses a zoneUpdater mixin to read the value of
the Select and pass it to an appropriate page class. The page class then
loads a new value from the DB and returns the zone so the update to the
TextField occurs. The missing element there is that the "disabled"
attribute is static.
What I am having a hard time figuring out is how to make a javascript
function call after my page class loads from the DB. The "disabled"
attribute depends on loading from the DB to determine if it should be
applied to the TextField or not.
I read through the recent e-mail thread 'Re: Worthy FAQ?: What is the
best way to return a zone update and a javascript as response to an XHR
request' as it seemed similar to what I am trying to achieve. In
particular I tried to work off Anna Vo's initial response quoted below:
------------------------------
You can inject ComponentResources, pass the zoneId and eventlink to
your javascript method with a JSONObject in your javascript
initializer call , and then call the tapestry zone update via client
side javascript.
Example Java Code:
@Inject
private ComponentResources resources;
@Environmental
private JavaScriptSupport javascriptSupport;
@InjectComponent
private Zone yourZone;
void afterRender()
{
Link url = resources.createEventLink("yourMethodName");
JSONObject spec = new JSONObject();
spec.put("zoneId", yourZone.getClientId());
spec.put("url", url.toString());
javascriptSupport.addInitializerCall(InitializationPriority.NORMAL,
"yourJsFunction", spec);
}
Object onYourMethodName()
{
return yourZone.getBody();
}
Example Javascript Code:
Tapestry.Initializer.yourJsFunction = function(spec)
{
....
var zone = Tapestry.findZoneManagerForZone(spec.zoneId);
zone.updateFromURL(spec.url);
....
};
------------------------------
JavaScriptSupport seems to be 5.2, so I replaced it with RenderSupport
and addInit (I hoped this was right). However, RenderSupport only being
available in the render phase means I cannot call it from the mixin
handler method, and BlackBird console reports that it is not available
from the Environment.
Here is a snippet of the page code handling the zoneUpdater mixin:
--------------------------------
@Inject
private Request _request;
@Inject
private RenderSupport javascriptSupport;
Object onChangeOfProduct(){
String product_id = _request.getParameter("param");
debug("In onChangeOfProduct. Product id: " + product_id);
type = product_id; //type is tied to the value of the Select component
setAmount(product_id);
return restZone.getBody();
}
void setAmount(String p){
if(p == null || "".equals(p.trim())){
debug("No product given to setAmount method");
return;
}
Product prod = (Product) pdao.read(p);
prodType = prod.getProductType(); //This is the basis for the conditional for the
"disabled" attribute
JSONObject spec = new JSONObject();
spec.put("disable", "Redeeming".equals(prodType));
javascriptSupport.addInit("disableRedeeming", spec);
amt = prod.getPointPrice().intValue(); //amt is tied to the value of
the TextField
debug("setAmount::New amt = " + amt);
}
------------------------------
Of course this code here doesn't work, but hopefully it at least
clarifies the logic I'm trying to work with. I capture the product id,
try to load it from the database, try to call the javascript to handle
the "disabled" attribute, and then update the value of the TextField.
Can someone point me in the right direction as to how one ought to
handle this scenario of needing to call javascript as a result of
processing a mixin?
Thanks,
Rich
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org