"BJörn Lindqvist" <[EMAIL PROTECTED]> writes:

> I think that it is less reusable because it can't contain stateful
> information. Let's take another example. I have an AutoCompleteField
> widget which you use like this:
>
>     field = AutoCompleteField(name = "myauto", search_controller =
> "dosearch", search_param = "input", result_name = "matches")
>
>     @expose(format = "json")
>     def dosearch(self, input):
>         return dict(matches = ["foo", "bar"])
>
> The Javascript in the widget calls the dosearch method and displays
> the returned matches list. Now I want to generalize the code inside
> dosearch, similar to how I generalized InstantTextField, so that it
> can be reused. I want a controller that can return autocompletion
> results for an arbitrary column in an arbitrary table:
>
> class CompleteController(Controller):
>     @expose(format = "json")
>     def complete(self, nr_completes, tablename, colname, input):
>         table = getattr(model, tablename)
>         cond = LIKE(getattr(table.q, colname), "%" + input + "%")
>         rows = table.select(cond)[:int(nr_completes)]
>         return dict(matches = [getattr(row, colname) for row in rows])
>
> As you can see, this won't work because AutoCompleteField expected its
> search_controller method only to take one argument by the name of the
> search_param variable. Maybe there is a good way to solve this
> problem? One way to solve it is ofcourse how to change how
> AutoCompleteField works, but if you have to do that, doesn't it prove
> that I'm right?

Hmmmm...  If you pass an sqlobject resultset you'd eliminate 'tablename' from
this list.  'input' is already provided, 'colname' is already there
('result_name').  So, you're only missing 'nr_completes' here.  

Also, subclassing and adding this extra parameter is not all that hard.  But I
believe that providing this only missing parameter would be nice, with it
defaulting to None and returning all matches.

> True, I didn't realise that. Then I opened two pages to the same url
> and wondered why one page seemed to update the other page in
> mysterious ways. :) So developing web apps is different from
> developing desktop apps.

I was bitten by that as well...  :-\

-- 
Jorge Godoy      <[EMAIL PROTECTED]>

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to