You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Rich M <ri...@moremagic.com> on 2010/09/21 19:46:15 UTC

[5.1.0.5] Firing javascript from page class from mixin handler method

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-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [5.1.0.5] Firing javascript from page class from mixin handler method

Posted by LLTYK <LL...@mailinator.com>.
My favorite reply, a mixin on a component in a zone will get it's javascript
run again on zone update. So you make a mixin that handles both disabling
and enabling, it will run on page render, and run again whenever you update
the zone.


Oh, and use renderSupport.addScript, like in here:
http://jumpstart.doublenegative.com.au/jumpstart/examples/javascript/creatingmixins1
-- 
View this message in context: http://tapestry-users.832.n2.nabble.com/5-1-0-5-Firing-javascript-from-page-class-from-mixin-handler-method-tp5556060p5558626.html
Sent from the Tapestry Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [5.1.0.5] Firing javascript from page class from mixin handler method

Posted by Rich M <ri...@moremagic.com>.
*sigh*

Pretty disappointed I didn't consider this, since it's exactly how I 
update the TextField's value.. but
disabled="${disabled}"  and then set the disabled value in the mixin 
handler method.

Although, I am still interested in learning how to get a call to some 
javascript from within a mixin handler if anyone knows.

Thanks,
Rich

On 09/21/2010 01:46 PM, Rich M wrote:
> 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-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org