I think we're clear now on the use of defer for for this task. The common question now seems to be about who does/is supposed to call start() and end(). In my example (as well as yours in less detail) I don't talk about who starts/ends and why. I simply cover why defer is needed. I think any inputs on these questions would be generally helpful:

1) Can you use only Heartbeat#defer without start()ing or end()ing?

2) If so, who start()s and end()s it?

3) If not, who must start() and end() it?

Kristian Marinkovic wrote:
sorry for jumping in but consider a label-input scenario/exampel ....

if you want to generate the "for" attribute for your label you dont know the id of the input field when the label is rendered before the field. therefore the
label components defers its rendering using the heartbeat service.

g,
kirs




Michael Courcy <[EMAIL PROTECTED]> 07.01.2008 16:17
Bitte antworten an
"Tapestry users" <users@tapestry.apache.org>


An
Tapestry users <users@tapestry.apache.org>
Kopie

Thema
Re: What's heartbeat ?







Hi Chris,

Chris Lewis a écrit :
I'm not an expert on this, but I can offer you a real need for defer(). Suppose you have a mixin that attaches some trivial piece of JavaScript to components that use it; for example, a key logger. As far as the JS, what you need to do is register a few DOM event handlers on an element - probably onkeypress or onkeyup, etc. Let's suppose you wrote a class that creates such objects, and all you need to instantiate them is the DOM id of the element.

In a simple case all you'd need to do is have your mixin include the javascript and provide a line (like support.addScript()) to instantiate the object in JS code - providing the component id, which I think you can get through something like ComponentResources. Now, if in your template code you have one component for which you explicitly provide the DOM id, then your mixin will end up with this id. However there are situations where you simply can't know the id before hand, and in such cases you must rely on Tapestry to provide it.

A classic example is looping. If you have a loop that for some reason creates a variable number of components, to which you'd like to attach instances of your JS key logger, then you need to get the ids (created on the fly) from Tapestry. What's more, you can't assk a component for its id until it has already been assigned one, and the only way to be sure that your code executes after the assignment happen is to use Heartbeat#defer.

Now as far as when a heartbeat is created, and when/who must call start() and end(), I'm not entirely sure. I think that a component whose behavior interacts with/depends on that of other components/mixins must use heartbeats to coordinate.
I understand this concept my question is how can they cooperate, It would be really nice to have a live example involving the use of heartbeat to get Ids of "only known at runtime component".

The only example I could find about heartbeat is to defer the firing of an event :
Taken from http://wiki.apache.org/tapestry/Tapestry5SubmitContextComponent

         Runnable sendNotification = new Runnable()
         {
             public void run()
             {
_resources.triggerEvent(SELECTED_EVENT, new Object[] {context}, null);
             }
         };

// When not deferred, don't wait, fire the event now (actually, at the end of the current // heartbeat). This is most likely because the Submit is inside a Loop and some contextual // information will change if we defer. Another option might be to wait until the next
         // heartbeak?

         if (_defer)
             _formSupport.defer(sendNotification);
         else
             _heartbeat.defer(sendNotification);


But If you read the rest of this exemple, _heartbeat.begin, _heartbeat.end are never called ... Who call them ? What means calling them ?
I hope this helps. I'd suggest looking into the source for heartbeat's implementation as well as what really happens inside form support's defer method. You're not alone in your questions and expanded docs on this would be helpful.
You're right I think a little dive in the source code would be refreshing :-)
sincerely,
chris

PS - irc.freenode.net #tapestry

Michael Courcy wrote:
Gabriel Landais a écrit :
Michael Courcy a écrit :
If you look at this exemple :  TreeGridComponent

http://tapestry5-treegrid.googlecode.com/svn/trunk/tapestry5-treegrid/src/main/java/org/codelutin/tapestry/components/TreeGrid.java
a hearbeat.begin is called in beginRender and heartbeat.end is called in afterRender but you can never read a call to defer, so can you explain why the author of this component call begin and end ? I really can't explain myself.
I've based my component on <http://wiki.apache.org/tapestry/Tapestry5HowToCreateYourOwnComponents and <http://wiki.apache.org/tapestry/Tapestry5TreeComponent> without really understanding how it heartbeat works... As I don't use defer, I don't even know if it is useful...
Gabriel
But do you see a different hehaviour if you remove this two lines ?
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





Reply via email to