You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by tomandjerry <sa...@gmail.com> on 2012/06/08 16:20:28 UTC

Zone Refresh with dynamic context

I would like to keep the lastUpdated time in the browser so that new events
can be propagated to the browser. For this I am using a Zone with
zonerefresh mixin where I am trying to dynamically change the context passed
to the ZoneRefresh mixin.

The below approach does not work. I appreciate if anyone has any other
better way of solving this problem.

<t:zone t:id="poller" elementName="span" period="5" t:mixins="zoneRefresh"
t:context="${time}">
		
</t:zone>

within the onRefresh event handler, I would like to change the 'time'  

Object onZoneRefresh(Object[] context) {
ajaxRenderer.addCallback(new JavaScriptCallback() {
						@Override
						public void run(JavaScriptSupport jsSupport) {
							JSONObject json = new JSONObject();
							json.put("period", 5);
                                                        json.put("URL",
createEventLink(newTime));
							jsSupport.addInitializerCall(InitializationPriority.LATE,
"zoneRefresh", json)
						}
					});
}

private Object createEventLink(Long newTime){
      Link link = resources.createEventLink("zoneRefresh", newTime);
      return link.toAbsoluteURI();
	 }

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Zone-Refresh-with-dynamic-context-tp5713719.html
Sent from the Tapestry - User 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: Zone Refresh with dynamic context

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
Instead of using context, use a request parameter instead, so you avoid  
having to deal with changing the URL Tapestry generated. In addition, I  
don't think you will be able to use the ZoneRefresh mixin, so you'll need  
a little bit of custom JavaScript, most probably adapted from  
ZoneRefresh's code.

On Fri, 08 Jun 2012 11:20:28 -0300, tomandjerry  
<sa...@gmail.com> wrote:

> I would like to keep the lastUpdated time in the browser so that new  
> events
> can be propagated to the browser. For this I am using a Zone with
> zonerefresh mixin where I am trying to dynamically change the context  
> passed
> to the ZoneRefresh mixin.
>
> The below approach does not work. I appreciate if anyone has any other
> better way of solving this problem.
>
> <t:zone t:id="poller" elementName="span" period="5"  
> t:mixins="zoneRefresh"
> t:context="${time}">
> 		
> </t:zone>
>
> within the onRefresh event handler, I would like to change the 'time'
>
> Object onZoneRefresh(Object[] context) {
> ajaxRenderer.addCallback(new JavaScriptCallback() {
> 						@Override
> 						public void run(JavaScriptSupport jsSupport) {
> 							JSONObject json = new JSONObject();
> 							json.put("period", 5);
>                                                         json.put("URL",
> createEventLink(newTime));
> 							jsSupport.addInitializerCall(InitializationPriority.LATE,
> "zoneRefresh", json)
> 						}
> 					});
> }
>
> private Object createEventLink(Long newTime){
>       Link link = resources.createEventLink("zoneRefresh", newTime);
>       return link.toAbsoluteURI();
> 	 }
>
> --
> View this message in context:  
> http://tapestry.1045711.n5.nabble.com/Zone-Refresh-with-dynamic-context-tp5713719.html
> Sent from the Tapestry - User 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
>


-- 
Thiago H. de Paula Figueiredo

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


Re: Zone Refresh with dynamic context

Posted by tomandjerry <sa...@gmail.com>.
JSONObject json = new JSONObject(); 
                                                        json.put("period",
5); 
                                                        json.put("URL",
createEventLink(newTime)); 
                                                        json.put("id",
"poller")); 
                                                       
jsSupport.addInitializerCall(InitializationPriority.LATE, "zoneRefresh",
json) 

Even with id, this does not work. If zone exists, periodic executor is not
created(looking at zone-refresh.js).
Perhaps I have to think in the lines of what Taha is suggesting

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Zone-Refresh-with-dynamic-context-tp5713719p5713737.html
Sent from the Tapestry - User 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: Zone Refresh with dynamic context

Posted by Lance Java <la...@googlemail.com>.
Firstly, Tapestry.Initializer.zoneRefresh requires an "id" to be present in
the json object.

Secondly, each time "zoneRefresh" is called, a PeriodicalExecuter is created
so you are likely creating more and more of these every time your zone is
refreshed until you run out of memory.

I think you are trying to do something that zonerefresh was not designed to
do. You could achieve this by calling window.setTimeout() once per refresh.
I'm sure prototype / jquery have other nicer options too.

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Zone-Refresh-with-dynamic-context-tp5713719p5713721.html
Sent from the Tapestry - User 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: Zone Refresh with dynamic context

Posted by Taha Siddiqi <ta...@gmail.com>.
I use my own implementation for Periodic display which displays a block till you return null from the event handler . It is for tapestry-jquery, so you will have to change the js from jquery to prototype. 

(I am not sure it will help because of the size of the code :). May be I will write a post about it tomorrow )

@Import(library = "ProgressiveDisplay2.js")
public class ProgressiveDisplay2 implements ClientElement {

  private static final String PROGRESSIVE_DISPLAY_INTERNAL =
          EventConstants.PROGRESSIVE_DISPLAY + "2";

  @Parameter(value = "5", defaultPrefix = BindingConstants.LITERAL)
  private int period;

  @Parameter
  private Object[] context;

  @Parameter(value = "prop:componentResources.id", defaultPrefix = BindingConstants.LITERAL,
          allowNull = false)
  private String clientId;

  private String assignedClientId;

  @SuppressWarnings("UnusedDeclaration")
  Object[] defaultContext() {
      return new Object[]{};
  }

  @Parameter(defaultPrefix = BindingConstants.BLOCK)
  @Property
  private Block initial;

  @SuppressWarnings("UnusedDeclaration")
  Block defaultInitial() {
      return initialDefault;
  }

  @Inject
  private JavaScriptSupport javaScriptSupport;

  @Inject
  private ComponentResources resources;

  @Inject
  private AjaxResponseRenderer ajaxResponseRenderer;

  @InjectComponent
  private Zone zone;

  @Inject
  private Block initialDefault;

  @SetupRender
  void assignClientId(){
      assignedClientId = javaScriptSupport.allocateClientId(clientId);
  }

  @AfterRender
  void addJavaScript() {
      JSONObject specs = getArguments(getClientId(), context);

      javaScriptSupport.addInitializerCall("ProgressiveDisplay2", specs);
  }

  private JSONObject getArguments(String zoneId, Object[] context) {
      JSONObject specs = new JSONObject();

      specs.put("url", createUpdateURI(context));
      specs.put("period", period);
      specs.put("zoneId", zoneId);

      return specs;
  }

  private String createUpdateURI(Object [] context) {
      return resources.createEventLink(PROGRESSIVE_DISPLAY_INTERNAL, context)
              .toAbsoluteURI();
  }

  @OnEvent(PROGRESSIVE_DISPLAY_INTERNAL)
  private Object update(final EventContext context, @RequestParameter("zoneId") final String zoneId) {
      CaptureResultCallback<Object> callback = new CaptureResultCallback<Object>();

      boolean handled = resources.triggerContextEvent(
              EventConstants.PROGRESSIVE_DISPLAY,
              context, callback);

      if (handled && callback.getResult() == null) {
          ajaxResponseRenderer.addCallback(new JavaScriptCallback() {

              @Override
              public void run(JavaScriptSupport javaScriptSupport) {
                  javaScriptSupport.addInitializerCall("ProgressiveDisplay2", 
                          getArguments(zoneId, context.toStrings()));
              }

          });

          return zone.getBody();
      }

      return callback.getResult();
  }

  @Override
  public String getClientId() {
      return assignedClientId;
  }

}

(function ($) {

  $.extend(Tapestry.Initializer, {
      ProgressiveDisplay2:function (specs) {

          window.setTimeout(function () {

              console.log("window.timeout : " + specs.zoneId);
              var zone = $("#" + specs.zoneId);
              var params = {zoneId : specs.zoneId};
              zone.tapestryZone('update', { url:specs.url, params:params });

          }, specs.period * 1000);

      }
  });

})(jQuery);

<t:container xmlns:t='http://tapestry.apache.org/schema/tapestry_5_3.xsd'>

  <div t:type='Zone' t:id='zone' id='${clientId}'>
      <div t:type='Delegate' t:to='initial'/>
  </div>

  <t:block t:id='initialDefault'>
      Loading...
  </t:block>

</t:container>

Usage :-

<div t:type='ProgressiveDisplay2' t:context='...'>
              <p:initial>
                  <img src='' class='loading_thumbnail' alt=''/>
              </p:initial>
          </div>


@OnEvent(EventConstants.PROGRESSIVE_DISPLAY)
  public Object onUpload(Media media) {
      this.media = media;
      return media.isUploaded() ? thumbnailBlock : null;
  }

regards
Taha




On Jun 8, 2012, at 7:50 PM, tomandjerry wrote:

> I would like to keep the lastUpdated time in the browser so that new events
> can be propagated to the browser. For this I am using a Zone with
> zonerefresh mixin where I am trying to dynamically change the context passed
> to the ZoneRefresh mixin.
> 
> The below approach does not work. I appreciate if anyone has any other
> better way of solving this problem.
> 
> <t:zone t:id="poller" elementName="span" period="5" t:mixins="zoneRefresh"
> t:context="${time}">
> 		
> </t:zone>
> 
> within the onRefresh event handler, I would like to change the 'time'  
> 
> Object onZoneRefresh(Object[] context) {
> ajaxRenderer.addCallback(new JavaScriptCallback() {
> 						@Override
> 						public void run(JavaScriptSupport jsSupport) {
> 							JSONObject json = new JSONObject();
> 							json.put("period", 5);
>                                                      json.put("URL",
> createEventLink(newTime));
> 							jsSupport.addInitializerCall(InitializationPriority.LATE,
> "zoneRefresh", json)
> 						}
> 					});
> }
> 
> private Object createEventLink(Long newTime){
>    Link link = resources.createEventLink("zoneRefresh", newTime);
>    return link.toAbsoluteURI();
> 	 }
> 
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Zone-Refresh-with-dynamic-context-tp5713719.html
> Sent from the Tapestry - User 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
> 


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