On Dec 11, 2006, at 12:39 AM, Matt Good wrote:
>
> I've started playing with ToscaWidgets is a Pylons app and I noticed a
> shorter way to define the child fields for a form if you don't need to
> reuse the fields in several forms.
>
> The examples all seem to define two classes like:
>
> class ContextFields(WidgetsList):
> name = TextField()
> description = TextArea()
>
> class ContextForm(ListForm):
> children = ContextFields
>
> Which can be rewritten as:
>
> class ContextForm(ListForm):
> class children(WidgetsList):
> name = TextField()
> description = TextArea()
>
> This looks nice at least for simple forms, and prevents polluting the
> module's namespace with classes not intended for public use.
It sure looks nicer! (and it's intentional ;)
However, there's one small catch:
The metaclass will move the inner class to Widget._cls_chidren (this
also occurs in the two-classes syntax) and when the widget instance
is initialized widget.children won't be a WidgetList anymore but a
WidgetBunch (so children can be accessed as w.children.name,
iterated, etc...)
This is mostly an internal implementation detail... just wanted to
point it out because this kind of metaclass black-magic sometimes
seems to confuse and/or annoy some users... for example, intuition
will fail if the inner class wants to be reused. You cannot do:
class NewFields(WidgetsList):
....
fields = NewFields + ContextForm.children
but you can:
fields = NewFields() + list(ContextForm().children) # notice the
widget is instatiated
Well, bottom line: If you need to reuse fields keep the WidgetsLists
in a class by themselves and "add" them (even the class itself) as
needed
Alberto
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---