[Cubicweb] Controller and message display without reloading

Florent Cayré florent.cayre at gmail.com
Wed Sep 5 21:37:36 CEST 2012


Hi Stéphane,

Le 31/08/2012 13:45, Stéphane Bugat a écrit :
> Dear CW users,
>
> Following a discussion we had auc & I on #public, here is a synthesis of my issue:
>
> I have developed a table view to display a list of contacts, and I added a search field to look for other contacts still not connected (by firstname, surname, and so on). For that, I have chosen to define a form with a single string widget and put a "form.render(...)" in the call method of the view.
>
> I added a controller to the form, of course. In some cases, if the rset resulting from the query using the search field is not empty, I redirect to another view with this controller. But if the rset is empty, I want my controller to stay on the same view but display a message in the notification bar, something like 'No results were found sorry'.

I would code this behaviour (redirection or message display) mostly 
client-side :

* your controller returns a json object (from a python dict) of the form :

    - {'empty': True, 'msg': 'MY MESSAGE'} if the rset is empty
    - {'empty': False, 'url': 'http://THE_URL'} in the opposite case

    Depending on the version of CW you are using -would be OK in current 
stable, 3.15.4-,
    you could take advantage of the ajaxfunc decorator found in 
cubicweb.web.views.ajaxcontroller
    (and well documented here), which would make developping your 
controller very easy,
    something like :

    @ajaxfunc(output_type='json')
    def my_ajaxfunc_controller:
       rset = self._cw.execute(MY_RQL_HERE)
       if rset:
           return {'empty': False, 'url': 'http://THE_URL'}
       else:
           return {'empty': True, 'msg': 'MY MESSAGE'}

* client-side, you call your controller and handle the above-mentionned 
returned value
    in your js callback, by testing the 'empty' value.
>
> And, the final point: I want this *without having to reload the whole view* (which can be a bit long depending on the query to get all contacts).
>
> How can I proceed?
>
> Stephane
> _______________________________________________
> Cubicweb mailing list
> Cubicweb at lists.cubicweb.org
> http://lists.cubicweb.org/mailman/listinfo/cubicweb

Hope this helps,
Florent.




More information about the Cubicweb mailing list