percious wrote:
> I think the idea here is the same as was behind "SessionConfig" in
> dbsprockets.
>
> That is, there is a standard set of data you expect to retrieve for a
> given generated widget, be it a set of data to populate drop-downs, or
> a jsonified stream to populate an autocomplete field. If there is a
> way we can make this data retrievable and customizable, we will have a
> win.
>
The plan is to query the repository or router for this information, so,
for example, to initialize a drop down you'll do:
@get_view.before("isinstance(attr, fields.Relation)")
def _add_options_and_validator(self, resource, attr, action, args):
if not attr.required:
options = [None]
else:
options = []
v = validators.RelatedFetcher(attr.other)
self.add_validator(v, args)
def get_options():
return options +
list(app.repositoryfactory(attr.other).select())
args['options'] = get_options
This code is from tw.rum. I'll explain: @get_view.before registers the
decorated function so it is executed before the main body of get_view is
called when the attribute we're visiting is a Relation. get_view is the
ViewFactory's method that returns a view *class* for an object or one of
it's attributes. The pattern you're seeing is used a lot: a "before"
action that modifies the "args" dict which is a dict of keyword
arguments that will be used to initialize the view (a widget in this
case). So the whole thing basically says: "when the attribute is a
relation, generate a list of options by querying the repository that
handles the related model for all it's items and add a validator that
will take care of converting the related object into and id (to display
in the drop-down) and back (to get the object from the repo. once we get
the id back).
If we wanted a fancy ajax lookup field instead of a drop-down we'll have
to tell it somehow where to fetch the items from... so instead of
querying the repository we ask the router who is the one who knows best
at what url endpoint each collection or member can be accessed from,
example:
args['url'] = app.url_for(attr.other, action='index', format='json')
BTW, app is a "rum.app" which is a proxy (StackedObjectProxy) which
points to the the active RumApp (ie: the one serving the current request
or the one we've explicitly put in context, more on this later...)
Alberto
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears Trunk" 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-trunk?hl=en
-~----------~----~----~----~------~----~------~--~---