[ 
https://issues.apache.org/jira/browse/TAP5-1407?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul Stanton updated TAP5-1407:
-------------------------------

    Description: 
Currently MultiZoneUpdate is a chain of MultiZoneUpdates, ie

public class MultiZoneUpdate
{
...
    public MultiZoneUpdate add(String zoneId, Object renderer)
    {
        return new MultiZoneUpdate(zoneId, renderer, this);
    }
...
}

usage:

MultiZoneUpdate mzu = new MultiZoneUpdate("zone2", zone1); // ugly!
mzu = mzu.add("zone2", zone2); // ugly!
mzu = mzu.add("zone3", zone3); // ugly!
...
return mzu;

This becomes hard to use when event handlers call common methods which 
contribute zone updates.

Also, it is possible to request multiple updates for the one zone which doesn't 
make much sense.

In some cases it would be much easier if you could construct a MultiZoneUpdate 
object without actually contributing a zone update directive. ie:

MultiZoneUpdate mzu = new MultiZoneUpdate();
mzu.add("zone2", zone1);
mzu.add("zone2", zone2);
mzu.add("zone3", zone3);
mzu.add("zone3", zone3); // knocks out prev zone3 update
...
return mzu;

I have created a utility class which helps me work around this issue (and issue 
#TAP5-1406), however note it relies on the dummy zone hack.:


import java.util.HashMap;
import java.util.Map.Entry;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.ajax.MultiZoneUpdate;
import org.apache.tapestry5.internal.services.PageRenderQueue;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.PartialMarkupRenderer;
import org.apache.tapestry5.services.PartialMarkupRendererFilter;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;

public class XHRResponseHelper
{
        private HashMap<String, Object> zoneUpdates;
        private boolean scriptAdded;

        public XHRResponseHelper()
        {
                this.zoneUpdates = new HashMap<String, Object>();
                scriptAdded = false;
        }

        public void addScriptCall(final String script, PageRenderQueue 
pageRenderQueue, final JavaScriptSupport javascriptSupport)
        {
                pageRenderQueue.addPartialMarkupRendererFilter(new 
PartialMarkupRendererFilter()
                {
                        public void renderMarkup(MarkupWriter writer, 
JSONObject reply, PartialMarkupRenderer renderer)
                        {
                                javascriptSupport.addScript(script);
                                renderer.renderMarkup(writer, reply);
                        }
                });
        }

        public void addZoneUpdate(String zoneId, ComponentResources 
componentResources)
        {
                addZoneUpdate(zoneId, 
componentResources.getEmbeddedComponent(zoneId));
        }

        public void addZoneUpdate(String zoneId, Object renderer)
        {
                zoneUpdates.put(zoneId, renderer);
        }

        public MultiZoneUpdate buildMultiZoneUpdate(ComponentResources 
componentResources)
        {
                // work around issue  - 
https://issues.apache.org/jira/browse/TAP5-1406
                if (zoneUpdates.isEmpty() && scriptAdded)
                        addZoneUpdate("dummyZone", componentResources);

                MultiZoneUpdate mzu = null;

                for (Entry<String, Object> entry : zoneUpdates.entrySet())
                {
                        if (mzu == null)
                                mzu = new MultiZoneUpdate(entry.getKey(), 
entry.getValue());
                        else
                                mzu.add(entry.getKey(), entry.getValue());
                }

                return mzu; // null if zoneUpdates is empty
        }
}

usage:

XHRResponseHelper response = new XHRResponseHelper();
response.addZoneUpdate("myZone", componentResources);
response.addZoneUpdate("myZone2", block);
response.addScriptCall("alert('script');", pageRenderQueue, javascriptSupport);
return response.buildMultiZoneUpdate(componentResources);


hope that helps.

  was:
Currently MultiZoneUpdate is a chain of MultiZoneUpdates, ie

public class MultiZoneUpdate
{
...
    public MultiZoneUpdate add(String zoneId, Object renderer)
    {
        return new MultiZoneUpdate(zoneId, renderer, this);
    }
...
}

usage:

MultiZoneUpdate mzu = new MultiZoneUpdate("zone2", zone1); // ugly!
mzu = mzu.add("zone2", zone2); // ugly!
mzu = mzu.add("zone3", zone3); // ugly!
...
return mzu;

This becomes hard to use when event handlers call common methods which 
contribute zone updates.

Also, it is possible to request multiple updates for the one zone which doesn't 
make much sense.

In some cases it would be much easier if you could construct a MultiZoneUpdate 
object without actually contributing a zone update directive. ie:

MultiZoneUpdate mzu = new MultiZoneUpdate();
mzu.add("zone2", zone1);
mzu.add("zone2", zone2);
mzu.add("zone3", zone3);
mzu.add("zone3", zone3); // knocks out prev zone3 update
...
return mzu;

in my utility class, i create a map of updates and construct the 
MultiZoneUpdate chain at the end.


> multizoneupdate should be easier to use - not a chain
> -----------------------------------------------------
>
>                 Key: TAP5-1407
>                 URL: https://issues.apache.org/jira/browse/TAP5-1407
>             Project: Tapestry 5
>          Issue Type: Improvement
>          Components: tapestry-core
>    Affects Versions: 5.2.4
>            Reporter: Paul Stanton
>             Fix For: 5.2.4
>
>
> Currently MultiZoneUpdate is a chain of MultiZoneUpdates, ie
> public class MultiZoneUpdate
> {
> ...
>     public MultiZoneUpdate add(String zoneId, Object renderer)
>     {
>         return new MultiZoneUpdate(zoneId, renderer, this);
>     }
> ...
> }
> usage:
> MultiZoneUpdate mzu = new MultiZoneUpdate("zone2", zone1); // ugly!
> mzu = mzu.add("zone2", zone2); // ugly!
> mzu = mzu.add("zone3", zone3); // ugly!
> ...
> return mzu;
> This becomes hard to use when event handlers call common methods which 
> contribute zone updates.
> Also, it is possible to request multiple updates for the one zone which 
> doesn't make much sense.
> In some cases it would be much easier if you could construct a 
> MultiZoneUpdate object without actually contributing a zone update directive. 
> ie:
> MultiZoneUpdate mzu = new MultiZoneUpdate();
> mzu.add("zone2", zone1);
> mzu.add("zone2", zone2);
> mzu.add("zone3", zone3);
> mzu.add("zone3", zone3); // knocks out prev zone3 update
> ...
> return mzu;
> I have created a utility class which helps me work around this issue (and 
> issue #TAP5-1406), however note it relies on the dummy zone hack.:
> import java.util.HashMap;
> import java.util.Map.Entry;
> import org.apache.tapestry5.ComponentResources;
> import org.apache.tapestry5.MarkupWriter;
> import org.apache.tapestry5.ajax.MultiZoneUpdate;
> import org.apache.tapestry5.internal.services.PageRenderQueue;
> import org.apache.tapestry5.json.JSONObject;
> import org.apache.tapestry5.services.PartialMarkupRenderer;
> import org.apache.tapestry5.services.PartialMarkupRendererFilter;
> import org.apache.tapestry5.services.javascript.JavaScriptSupport;
> public class XHRResponseHelper
> {
>       private HashMap<String, Object> zoneUpdates;
>       private boolean scriptAdded;
>       public XHRResponseHelper()
>       {
>               this.zoneUpdates = new HashMap<String, Object>();
>               scriptAdded = false;
>       }
>       public void addScriptCall(final String script, PageRenderQueue 
> pageRenderQueue, final JavaScriptSupport javascriptSupport)
>       {
>               pageRenderQueue.addPartialMarkupRendererFilter(new 
> PartialMarkupRendererFilter()
>               {
>                       public void renderMarkup(MarkupWriter writer, 
> JSONObject reply, PartialMarkupRenderer renderer)
>                       {
>                               javascriptSupport.addScript(script);
>                               renderer.renderMarkup(writer, reply);
>                       }
>               });
>       }
>       public void addZoneUpdate(String zoneId, ComponentResources 
> componentResources)
>       {
>               addZoneUpdate(zoneId, 
> componentResources.getEmbeddedComponent(zoneId));
>       }
>       public void addZoneUpdate(String zoneId, Object renderer)
>       {
>               zoneUpdates.put(zoneId, renderer);
>       }
>       public MultiZoneUpdate buildMultiZoneUpdate(ComponentResources 
> componentResources)
>       {
>               // work around issue  - 
> https://issues.apache.org/jira/browse/TAP5-1406
>               if (zoneUpdates.isEmpty() && scriptAdded)
>                       addZoneUpdate("dummyZone", componentResources);
>               MultiZoneUpdate mzu = null;
>               for (Entry<String, Object> entry : zoneUpdates.entrySet())
>               {
>                       if (mzu == null)
>                               mzu = new MultiZoneUpdate(entry.getKey(), 
> entry.getValue());
>                       else
>                               mzu.add(entry.getKey(), entry.getValue());
>               }
>               return mzu; // null if zoneUpdates is empty
>       }
> }
> usage:
> XHRResponseHelper response = new XHRResponseHelper();
> response.addZoneUpdate("myZone", componentResources);
> response.addZoneUpdate("myZone2", block);
> response.addScriptCall("alert('script');", pageRenderQueue, 
> javascriptSupport);
> return response.buildMultiZoneUpdate(componentResources);
> hope that helps.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to