开发者

Multiple instances of JQueryUI Autocomplete on same Pyramid page

I have a working JQueryUI开发者_C百科 Autocomplete input widget working nicely with my Pyramid backend. The autocomplete posts its request.params['term'] to the same URL as the page its on, and Pyramid uses request_param='term' as a route predicate to send the term value to my database query view-callable.

The problem I have now though, is that I need to add more autocomplete widgets to the same page. However, I can't use the same route predicate term because both widgets post term and so Pyramid doesn't know where to send the post.

I know you can make custom predicates in Pyramid, but as far as I can see, there is nothing unique about the 2 autocomplete widgets with which I can make a custom predicate.

To give some context, below is the route/view definition. The definition is actually illegal because Pyramid rightly refused to identical predicates, but it serves to demonstrate my dilemma:

config.add_route('search_programs','/search/programs')
config.add_view(
        'haystack.search.search_programs',
        route_name='search_programs',
        renderer='templates/search_programs.jinja2')
config.add_view(
        'haystack.search.programtype_autocomplete',
        route_name='search_programs',
        request_param='term', renderer='json')
config.add_view(
        'haystack.search.majorgenre_autocomplete',
        route_name='search_programs',
        request_param='term', renderer='json')

What would be awesome is if the autocomplete widget had an option to specify an arbitrary key for posting, instead of just 'term' every time. Any help would be awesome.


You need to set your autocomplete for each widget explicitly and indicate a specific url and/or a custom piece of data in the post.

This is a clip from the Remote JSONP datasource example on the JQueryUI documentation:

$( "#a-particular-auto-complete-widget" ).autocomplete({
    source: function( request, response ) {
        $.ajax({ 
                 /* specifiy your specific url here - could be a different 
                  * route for each different source....
                  */
                 url: "http://someurl.com/search/programs/majorgenre",
                 dataType: "jsonp",
                 data: { featureClass: "P",
                         style: "full",
                         maxRows: 12,
                         /* ...or add a custom piece of data to 
                          * indicate how it should be handled by 
                          * your Pyramid view(s).
                          */
                         customParam: "majorgenre",
                         name_startsWith: request.term
                       }
 /* see docs for rest of code... */
 .... 

Then you can handle it in Pyramid however you want. I would probably just map one view to this route (and not bother with an ajax customParam):

config.add_route('search_programs','/search/programs/{autocomplete}')

And then in the view:

def autocomplete_handler_view(request):
    autocomplete_type = request.matchdict.get('autocomplete', None)
    if autocomplete == 'majorgenre':
        return handle_majorgenre(request.params['term'])

    elif autocomplete == 'programtype':
        return handle_programtype(request.params['term'])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜