Re: Reusable Components - one last question
Re: Reusable Components - one last question
- Subject: Re: Reusable Components - one last question
- From: email@hidden
- Date: Mon, 20 Oct 2003 11:25:17 CDT
- Priority: 3 (Normal)
[demime could not interpret encoding binary - treating as plain text]
The only communication between parent and child is through the bindings.
(Well, there's one or two exceptions to this, but this is probably the
best way to think about it anyway). There's no way for the parent to call
a method in the child---the parent can just pass bindings to the child,
and receive values back through the bindings as well.
There are two basic ways for bindings to be sent back and forth. The most
basic is to let WO do it for you---this is the default. When WO does it
for you, I believe that bindings are exchanged at the beginning and end
of each of the request-response control points: appendToResponse,
takeValuesFromRequest, and invokeAction. However, I don't know the
details of exactly how and when this works----my general principle is
that if you find yourself needing to know the details, you should move on
to the second way of sending bindings back and forth.
For the second way, you override the synchronizesVariablesWithBindings()
method in your component to return false. This method is somewhat
misleadingly named in my opinion; it really should be called something
like automaticallySynchronizesVariablesWithBindings. Once you've
over-ridden this method to return false, the binding values will NEVER be
obtained or set for you. You are responsible for all binding value
getting and setting yourself, manually, using valueForBinding and
setValueForBinding. This gives you complete control over when and how
the bindings are exchanged: this allows you to write a more efficient
sub-component (that isn't doing unneccesary binding exchanges all the
time! The default 'automatic' binding synchornization is quite energetic
about this); it also allows you to write components with binding
functionality that would be dificult or impossible with the default
automatic mechanism.
Whenever the default automatic mechanism isn't quite working for you, I
reccomend switching to the manual binding scheme.
Where is the best place to get these binding values? There are a number
of ways to do it. I generally use one of two, depending on what seems
easiest or best for the component in question:
1) Write a method loadBindings() which loads all binding values, and call
this method in an over-ridden appendToResponse, prior to calling the
superclass implementation.
2) "Lazy" loading of binding values. For instance, a method:
public Object someBindingValue() {
if ( someBindingValue == null ) {
// load on demand, when needed and not present:
someBindingValue = valueForBinding("someBindingValue");
}
return someBindingValue;
}
If using the lazy method, you may want to reset your values to null at
some point in the r-r loop, if you want the value to be able to change
between one r-r loop and another. In general, I prefer method 1, because
it accomplishes this already.
For bindings values that I want to send _out_, from the child TO the
parent, I just call setValueForBinding as appropriate, generally whenever
that value has been acquired, in whatever method is convenient.
In general, you will NOT find binding values available in the
constructor. You need to wait until you are fully initialized in a
request-response loop, and I don't believe constructor time counts as
that. Using the 'lazy initialization' pattern discussed above usually
helps you not even need to think about exaclty when the bindings become
available---that lazy initialization pattern isn't just for binding
values. I prefer to load all my binding values in appendToResponse, but
the general pattern of 'lazy initialization'---don't try to
load/calculate values until you need them---generally helps me avoid even
having to worry about exactly when the values become available. As long
as they are available by the time I need them, and they pretty much
always are. [Since by the time you need them, is in the request-response
loop].
Hope this helps as an introduction.
--Jonathan
On Mon, 20 Oct 2003 10:48:08 +1300 Ray Ackland wrote:
> With the help and input from Andrew (and Art), my page is continuing to
> progress and I am feeling quite pleased with my new knowledge.
>
> The thing I am wanting to do now (aren't I ever satisfied?) is to
> access methods in the sub-component. For instance, the parent provides
> the child with the name of an entity. The child then needs to do a
> fetch and perform calculations to get the result to display.
>
> I would like to know where I should call those methods? In particular,
> at what stage are the binded variables set? I have tried calling the
> methods from the constructor (didn't really expect it to work), and
> from the awake() method.
>
> Each time the bound variable was still null. I also added the method
> ensureAwakeInContext() but this didn't seem to have an effect.
>
> My end result was to use the method valueForBinding() to set them
> explicitly, but is this really necessary?
>
> Any suggestions?
>
> ray.
> _______________________________________________
> webobjects-dev mailing list | email@hidden
> Help/Unsubscribe/Archives:
> http://www.lists.apple.com/mailman/listinfo/webobjects-dev
> Do not post admin requests to the list. They will be ignored.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.