[request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object. stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 04 Aug 2010 10:47:29 +0200
branchstable
changeset 6062 f1a550102f5c
parent 6061 7cb29eab1c9d
child 6063 17a53f6dea42
[request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
web/request.py
--- a/web/request.py	Wed Aug 04 10:22:11 2010 +0200
+++ b/web/request.py	Wed Aug 04 10:47:29 2010 +0200
@@ -37,7 +37,7 @@
 from cubicweb.dbapi import DBAPIRequest
 from cubicweb.mail import header
 from cubicweb.uilib import remove_html_tags
-from cubicweb.utils import SizeConstrainedList, HTMLHead, make_uid, json_dumps
+from cubicweb.utils import SizeConstrainedList, HTMLHead, make_uid, js
 from cubicweb.view import STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE_NOEXT
 from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit,
                           RequestError, StatusResponse)
@@ -342,23 +342,37 @@
             return breadcrumbs.pop()
         return self.base_url()
 
-    def user_rql_callback(self, args, msg=None):
+    def user_rql_callback(self, rqlargs, *args, **kwargs):
         """register a user callback to execute some rql query and return an url
-        to call it ready to be inserted in html
+        to call it ready to be inserted in html.
+
+        rqlargs should be a tuple containing argument to give to the execute function.
+
+        For other allowed arguments, see :meth:`user_callback` method
         """
         def rqlexec(req, rql, args=None, key=None):
             req.execute(rql, args, key)
-        return self.user_callback(rqlexec, args, msg)
+        return self.user_callback(rqlexec, rqlargs, *args, **kwargs)
+
+    def user_callback(self, cb, cbargs, *args, **kwargs):
+        """register the given user callback and return an url to call it ready
+        to be inserted in html.
 
-    def user_callback(self, cb, args, msg=None, nonify=False):
-        """register the given user callback and return an url to call it ready to be
-        inserted in html
+        You can specify the underlying js function to call using a 'jsfunc'
+        named args, to one of :func:`userCallback`,
+        ':func:`userCallbackThenUpdateUI`, ':func:`userCallbackThenReloadPage`
+        (the default). Take care arguments may vary according to the used
+        function.
         """
         self.add_js('cubicweb.ajax.js')
-        cbname = self.register_onetime_callback(cb, *args)
-        msg = json_dumps(msg or '')
-        return "javascript:userCallbackThenReloadPage('%s', %s)" % (
-            cbname, msg)
+        jsfunc = kwargs.pop('jsfunc', 'userCallbackThenReloadPage')
+        if 'msg' in kwargs:
+            warn('[3.10] msg should be given as positional argument',
+                 DeprecationWarning, stacklevel=2)
+            args = (kwargs.pop('msg'),) + args
+        assert not kwargs, 'dunno what to do with remaining kwargs: %s' % kwargs
+        cbname = self.register_onetime_callback(cb, *cbargs)
+        return "javascript: %s" % getattr(js, jsfunc)(cbname, *args)
 
     def register_onetime_callback(self, func, *args):
         cbname = 'cb_%s' % (
@@ -589,8 +603,8 @@
         """
         extraparams.setdefault('fname', 'view')
         url = self.build_url('json', **extraparams)
-        return "javascript: $('#%s').loadxhtml(%s, null, 'get', '%s'); noop()" % (
-                nodeid, json_dumps(url), replacemode)
+        return "javascript: $('#%s').%s; noop()" % (
+            nodeid, js.loadxtml(url, None, 'get', replacemode)
 
     # urls/path management ####################################################