You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Entropy <bl...@gmail.com> on 2014/07/18 18:38:26 UTC

Behavior to modify attributes

I have a third party js library I am applying to my page.  I have to mark the
fields I want excluded with a particular HTML attribute.  So I am
implementing a custom behavior to do the work, and I want the behavior to
act like it's an opt-in behavior instead of opt-out.

So I wrote my behavior to take a set of components in it's constructor and
then, I want it to iterate through the text areas and text fields on the
components to mark the ones NOT provided wit the opt-out attribute.  

However, when I add an AttributeModifier to a tag in beforeRender I get an
exception:

org.apache.wicket.WicketRuntimeException: Cannot modify component hierarchy
after render phase has started (page version cant change then anymore)
	at org.apache.wicket.Component.checkHierarchyChange(Component.java:3595)
~[wicket-core-6.12.0.jar:6.12.0]

I could move the code to the bind I suppose, but then user's of my component
would have to make sure they added the behavior after ALL components, even
the ones that are going to be excluded, have been created.

Is there an event between these two?  Something the behavior can hook into
to do it's work after the construction of the components would be done, but
before beforeREnder since that seems to create issues?

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Behavior-to-modify-attributes-tp4666687.html
Sent from the Users forum mailing list archive at Nabble.com.

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


Re: Behavior to modify attributes

Posted by Entropy <bl...@gmail.com>.
Oh, I just tried it in my common page's onBeforeRender.  Evidently I can do
this from the page.onBeforeRender, just not from the behavior
onBeforeRender.  Which is probably what you meant.  Okay, I can work with
that.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Behavior-to-modify-attributes-tp4666687p4666715.html
Sent from the Users forum mailing list archive at Nabble.com.

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


Re: Behavior to modify attributes

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

You are using Behavior in a strange way.
component.add(behavior) will call behavior#onBind(component) because Wicket
knows that the behavior is used by this component.
Later when Wicket starts to render the components it calls the Behavior's
methods like: beforeRender(), afterRender(), etc.
What's strange in your approach is that a behavior is added to one
component (the page?!) that is completely unrelated to the main task and it
holds references to some other components which should be manipulated
somehow ...
Better mark these components somehow (annotation, marker interface, base
class, ...) and use IComponentInstantiationListener to add the
AttributeModifier to all such components.

 Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov


On Mon, Jul 21, 2014 at 2:45 PM, Entropy <bl...@gmail.com> wrote:

> > Instead of doing:
> >   page.add(new MyBehavior(components))
> > do
> >  Behavior b = AttributeModifier.append(....);
> >  for (Component c : components) {
> >     c.add(b)
> >  }
>
> Certainly that would work,  I am trying to create a re-usable Behavior for
> my app that any page wishing to incorporate this feature can add.  I
> thought
> behaviors would be perfect for this, but it seems one event is to early
> (the
> components may not have been all added yet) and another too late (not
> allowed to add AttributeModifiers).
>
> I have a common abstract page class from which all my classes extend, and I
> certainly could put a method on there, but I'd be relying on the individual
> programmer to call the method last in his constructor, rather than relying
> on an event which guarantees it.
>
> Seems like there ought to be a goldilocks place to put this...not too
> early,
> not too late.
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Behavior-to-modify-attributes-tp4666687p4666714.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Behavior to modify attributes

Posted by Entropy <bl...@gmail.com>.
> Instead of doing: 
>   page.add(new MyBehavior(components)) 
> do 
>  Behavior b = AttributeModifier.append(....); 
>  for (Component c : components) { 
>     c.add(b) 
>  } 

Certainly that would work,  I am trying to create a re-usable Behavior for
my app that any page wishing to incorporate this feature can add.  I thought
behaviors would be perfect for this, but it seems one event is to early (the
components may not have been all added yet) and another too late (not
allowed to add AttributeModifiers).

I have a common abstract page class from which all my classes extend, and I
certainly could put a method on there, but I'd be relying on the individual
programmer to call the method last in his constructor, rather than relying
on an event which guarantees it.

Seems like there ought to be a goldilocks place to put this...not too early,
not too late.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Behavior-to-modify-attributes-tp4666687p4666714.html
Sent from the Users forum mailing list archive at Nabble.com.

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


Re: Behavior to modify attributes

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov


On Fri, Jul 18, 2014 at 7:38 PM, Entropy <bl...@gmail.com> wrote:

> I have a third party js library I am applying to my page.  I have to mark
> the
> fields I want excluded with a particular HTML attribute.  So I am
> implementing a custom behavior to do the work, and I want the behavior to
> act like it's an opt-in behavior instead of opt-out.
>
> So I wrote my behavior to take a set of components in it's constructor and
> then, I want it to iterate through the text areas and text fields on the
> components to mark the ones NOT provided wit the opt-out attribute.
>
> However, when I add an AttributeModifier to a tag in beforeRender I get an
> exception:
>

Instead of doing:
  page.add(new MyBehavior(components))
do
  Behavior b = AttributeModifier.append(....);
  for (Component c : components) {
     c.add(b)
  }


>
> org.apache.wicket.WicketRuntimeException: Cannot modify component hierarchy
> after render phase has started (page version cant change then anymore)
>         at
> org.apache.wicket.Component.checkHierarchyChange(Component.java:3595)
> ~[wicket-core-6.12.0.jar:6.12.0]
>
> I could move the code to the bind I suppose, but then user's of my
> component
> would have to make sure they added the behavior after ALL components, even
> the ones that are going to be excluded, have been created.
>
> Is there an event between these two?  Something the behavior can hook into
> to do it's work after the construction of the components would be done, but
> before beforeREnder since that seems to create issues?
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Behavior-to-modify-attributes-tp4666687.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>