when request is a json request, bind on 'ajax-loaded' instead of document.ready() stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 03 Jul 2009 18:00:07 +0200
branchstable
changeset 2258 79bc598c6411
parent 2257 2c9eceeaae6a
child 2259 5d90bd26af8b
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
utils.py
web/request.py
web/views/tabs.py
--- a/utils.py	Fri Jul 03 17:58:36 2009 +0200
+++ b/utils.py	Fri Jul 03 18:00:07 2009 +0200
@@ -206,8 +206,13 @@
     def add_post_inline_script(self, content):
         self.post_inlined_scripts.append(content)
 
-    def add_onload(self, jscode):
-        self.add_post_inline_script(u"""jQuery(document).ready(function () {
+    def add_onload(self, jscode, jsoncall=False):
+        if jsoncall:
+            self.add_post_inline_script(u"""jQuery(CubicWeb).bind('ajax-loaded', function(event) {
+%s
+});""" % jscode)
+        else:
+            self.add_post_inline_script(u"""jQuery(document).ready(function () {
  %s
  });""" % jscode)
 
--- a/web/request.py	Fri Jul 03 17:58:36 2009 +0200
+++ b/web/request.py	Fri Jul 03 18:00:07 2009 +0200
@@ -453,6 +453,9 @@
 
     # high level methods for HTML headers management ##########################
 
+    def add_onload(self, jscode):
+        self.html_headers.add_onload(jscode, self.json_request)
+
     def add_js(self, jsfiles, localfile=True):
         """specify a list of JS files to include in the HTML headers
         :param jsfiles: a JS filename or a list of JS filenames
--- a/web/views/tabs.py	Fri Jul 03 17:58:36 2009 +0200
+++ b/web/views/tabs.py	Fri Jul 03 18:00:07 2009 +0200
@@ -24,7 +24,7 @@
     """
 
     def _prepare_bindings(self, vid, reloadable):
-        self.req.html_headers.add_onload(u"""
+        self.req.add_onload(u"""
   jQuery('#lazy-%(vid)s').bind('%(event)s', function(event) {
      load_now('#lazy-%(vid)s', '#%(vid)s-hole', %(reloadable)s);
   });""" % {'event': 'load_%s' % vid, 'vid': vid,
@@ -59,7 +59,7 @@
         on dom readyness
         """
         self.req.add_js('cubicweb.lazy.js')
-        self.req.html_headers.add_onload("trigger_load('%s');" % vid)
+        self.req.add_onload("trigger_load('%s');" % vid)
 
 
 class TabsMixin(LazyViewMixin):
@@ -143,7 +143,7 @@
             w(u'</div>')
         # call the set_tab() JS function *after* each tab is generated
         # because the callback binding needs to be done before
-        self.req.html_headers.add_onload(u"""
+        self.req.add_onload(u"""
    jQuery('#entity-tabs-%(eeid)s > ul').tabs( { selected: %(tabindex)s });
    set_tab('%(vid)s', '%(cookiename)s');
  """ % {'tabindex'   : tabs.index(active_tab),