doc/book/en/development/devweb/js.rst
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Tue, 06 Apr 2010 15:11:51 +0200
branchstable
changeset 5151 2f70fa8b6854
parent 4830 10e8bc190695
child 5152 35e6878e2fd0
permissions -rw-r--r--
[doc/book] expand the js chapter with an ajax story
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
.. -*- coding: utf-8 -*-
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     2
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     3
Javascript
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     4
----------
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     5
3283
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
     6
*CubicWeb* uses quite a bit of javascript in its user interface and
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
     7
ships with jquery (1.3.x) and parts of the jquery UI
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
     8
library, plus a number of homegrown files and also other thirparty
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
     9
libraries.
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    10
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    11
All javascript files are stored in cubicweb/web/data/. There are
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    12
around thirty js files there. In a cube it goes to data/.
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    13
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    14
Obviously one does not want javascript pieces to be loaded all at
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    15
once, hence the framework provides a number of mechanisms and
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    16
conventions to deal with javascript resources.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    17
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    18
Conventions
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    19
~~~~~~~~~~~
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    20
3283
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    21
It is good practice to name cube specific js files after the name of
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    22
the cube, like this : 'cube.mycube.js', so as to avoid name clashes.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    23
3283
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    24
XXX external_resources variable (which needs love)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    25
3441
bdeb91e08278 typo fix
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 3283
diff changeset
    26
CubicWeb javascript api
3581
669854258b90 [doc] various fixes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3441
diff changeset
    27
~~~~~~~~~~~~~~~~~~~~~~~
3283
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    28
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    29
Javascript resources are typically loaded on demand, from views. The
4437
21f2e01fdd6a update exemples using the 3.6 api and add/fix some sections (schema, vreg, talk about CW_MODE in concepts...). So much to do :'(
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3581
diff changeset
    30
request object (available as self._cw from most application objects,
3283
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    31
for instance views and entities objects) has a few methods to do that:
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    32
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    33
* `add_js(self, jsfiles, localfile=True)` which takes a sequence of
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    34
  javascript files and writes proper entries into the HTML header
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    35
  section. The localfile parameter allows to declare resources which
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    36
  are not from web/data (for instance, residing on a content delivery
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    37
  network).
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    38
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    39
* `add_onload(self, jscode)` which adds one raw javascript code
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    40
  snippet inline in the html headers. This is quite useful for setting
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    41
  up early jQuery(document).ready(...) initialisations.
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    42
4830
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    43
CubicWeb javascript events
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    44
~~~~~~~~~~~~~~~~~~~~~~~~~~
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    45
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    46
* ``server-response``: this event is triggered on HTTP responses (both
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    47
  standard and ajax). The two following extra parameters are passed
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    48
  to callbacks :
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    49
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    50
  - ``ajax``: a boolean that says if the reponse was issued by an
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    51
    ajax request
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    52
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    53
  - ``node``: the DOM node returned by the server in case of an
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    54
    ajax request, otherwise the document itself for standard HTTP
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    55
    requests.
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    56
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    57
Important AJAX APIS
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    58
~~~~~~~~~~~~~~~~~~~
4830
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    59
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    60
* `jQuery.fn.loadxhtml` is an important extension to jQuery which
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    61
  allow proper loading and in-place DOM update of xhtml views. It is
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    62
  suitably augmented to trigger necessary events, and process CubicWeb
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    63
  specific elements such as the facet system, fckeditor, etc.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    64
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    65
* `asyncRemoteExec` and `remoteExec` are the base building blocks for
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    66
  doing arbitrary async (resp. sync) communications with the server
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    67
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    68
A simple example with asyncRemoteExec
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    69
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    70
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    71
In the python side, we have to extend the BaseController class. The
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    72
@jsonize decorator ensures that the `return value` of the method is
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    73
encoded as JSON data. By construction, the JSonController inputs
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    74
everything in JSON format.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    75
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    76
.. sourcecode: python
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    77
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    78
    from cubicweb.web.views.basecontrollers import JSonController, jsonize
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    79
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    80
    @monkeypatch(JSonController)
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    81
    @jsonize
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    82
    def js_say_hello(self, name):
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    83
        return u'hello %s' % name
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    84
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    85
In the javascript side, we do the asynchronous call. Notice how it
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    86
creates a `deferred` object. Proper treatment of the return value or
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    87
error handling has to be done through the addCallback and addErrback
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    88
methods.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    89
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    90
.. sourcecode: javascript
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    91
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    92
    function async_hello(name) {
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    93
        var deferred = asyncRemoteExec('say_hello', name);
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    94
        deferred.addCallback(function (response) {
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    95
            alert(response);
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    96
        });
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    97
        deferred.addErrback(function () {
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    98
            alert('something fishy happened');
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    99
        });
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   100
     }
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   101
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   102
     function sync_hello(name) {
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   103
         alert( remoteExec('say_hello', name) );
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   104
     }
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   105
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   106
A simple example with loadxhtml
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   107
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   108
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   109
Here we are concerned with the retrieval of a specific view to be
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   110
injected in the live DOM. The view will be of course selected
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   111
server-side using an entity eid provided by the client side.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   112
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   113
.. sourcecode: python
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   114
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   115
    from cubicweb import typed_eid
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   116
    from cubicweb.web.views.basecontrollers import JSonController, xhtmlize
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   117
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   118
    @monkeypatch(JSonController)
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   119
    @xhtmlize
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   120
    def js_frob_status(self, eid, frobname):
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   121
        entity = self._cw.entity_from_eid(typed_eid(eid))
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   122
        return entity.view('frob', name=frobname)
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   123
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   124
.. sourcecode: javascript
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   125
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   126
    function update_some_div(divid, eid, frobname) {
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   127
        var params = {fname:'frob_status', eid: eid, frobname:frobname};
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   128
        jQuery('#'+divid).loadxhtml(JSON_BASE_URL, params, 'post');
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   129
     }
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   130
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   131
In this example, the url argument is the base json url of a cube
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   132
instance (it should contain something like
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   133
`http://myinstance/json?`). The actual JSonController method name is
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   134
encoded in the `params` dictionnary using the `fname` key.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   135
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   136
A more real-life example from CubicWeb
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   137
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   138
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   139
A frequent use case of Web 2 applications is the delayed (or
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   140
on-demand) loading of pieces of the DOM. This is typically achieved
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   141
using some preparation of the initial DOM nodes, jQuery event handling
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   142
and proper use of loadxhtml.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   143
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   144
We present here a skeletal version of the mecanism used in CubicWeb
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   145
and available in web/views/tabs.py, in the `LazyViewMixin` class.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   146
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   147
.. sourcecode: python
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   148
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   149
    def lazyview(self, vid, rql=None):
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   150
        """ a lazy version of wview """
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   151
        w = self.w
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   152
        self._cw.add_js('cubicweb.lazy.js')
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   153
        urlparams = {'vid' : vid, 'fname' : 'view'}
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   154
        if rql is not None:
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   155
            urlparams['rql'] = rql
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   156
        w(u'<div id="lazy-%s" cubicweb:loadurl="%s">' % (
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   157
            vid, xml_escape(self._cw.build_url('json', **urlparams))))
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   158
        w(u'</div>')
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   159
        self._cw.add_onload(u"""
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   160
            jQuery('#lazy-%(vid)s').bind('%(event)s', function() {
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   161
                   load_now('#lazy-%(vid)s');});"""
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   162
            % {'event': 'load_%s' % vid, 'vid': vid})
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   163
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   164
This creates a `div` with an specific event associated to it.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   165
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   166
The full version deals with:
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   167
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   168
* optional parameters such as an rql expression, an entity eid, an
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   169
  rset
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   170
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   171
* the ability to further reload the fragment
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   172
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   173
* the ability to display a spinning wheel while the fragment is still
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   174
  not loaded
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   175
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   176
* handling of browsers that do not support ajax (search engines,
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   177
  text-based browsers such as lynx, etc.)
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   178
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   179
The javascript side is quite simple, due to loadxhtml awesomeness.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   180
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   181
.. sourcecode: javascript
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   182
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   183
    function load_now(eltsel) {
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   184
        var lazydiv = jQuery(eltsel);
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   185
        lazydiv.loadxhtml(lazydiv.attr('cubicweb:loadurl'));
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   186
    }
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   187
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   188
This is all significantly different of the previous `simple example`
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   189
(albeit this example actually comes from real-life code).
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   190
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   191
Notice how the `cubicweb:loadurl` is used to convey the url
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   192
information. The base of this url is similar to the global javascript
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   193
JSON_BASE_URL. According to the pattern described earlier,
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   194
the `fname` parameter refers to the standard `js_view` method of the
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   195
JSonController. This method renders an arbitrary view provided a view
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   196
id (or `vid`) is provided, and most likely an rql expression yielding
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   197
a result set against which a proper view instance will be selected.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   198
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   199
The `cubicweb:loadurl` is one of the 29 attributes extensions to XHTML
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   200
in a specific cubicweb namespace. It is a means to pass information
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   201
without breaking HTML nor XHTML compliance and without resorting to
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   202
ungodly hacks.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   203
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   204
Given all this, it is easy to add a small nevertheless useful feature
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   205
to force the loading of a lazy view (for instance, a very
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   206
computation-intensive web page could be scinded into one fast-loading
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   207
part and a delayed part).
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   208
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   209
In the server side, a simple call to a javascript function is
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   210
sufficient.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   211
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   212
.. sourcecode: python
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   213
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   214
    def forceview(self, vid):
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   215
        """trigger an event that will force immediate loading of the view
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   216
        on dom readyness
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   217
        """
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   218
        self._cw.add_onload("trigger_load('%s');" % vid)
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   219
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   220
The browser-side definition follows.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   221
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   222
.. sourcecode: javascript
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   223
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   224
    function trigger_load(divid) {
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   225
        jQuery('#lazy-' + divd).trigger('load_' + divid);
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   226
    }
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   227
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   228
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   229
Anatomy of a lodxhtml call
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   230
~~~~~~~~~~~~~~~~~~~~~~~~~~
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   231
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   232
The loadxhtml extension to jQuery accept many parameters with rich
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   233
semantics. Let us detail these.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   234
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   235
* `url` (mandatory) should be a complete url, typically based on the
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   236
  JSonController, but this is not strictly mandatory
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   237
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   238
* `data` (optional) is a dictionnary of values given to the
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   239
  controller specified through an `url` argument; some keys may have a
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   240
  special meaning depending on the choosen controller (such as `fname`
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   241
  for the JSonController); the `callback` key, if present, must refer
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   242
  to a function to be called at the end of loadxhtml (more on this
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   243
  below)
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   244
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   245
* `reqtype` (optional) specifies the request method to be used (get or
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   246
  post); if the argument is 'post', then the post method is used,
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   247
  otherwise the get method is used
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   248
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   249
* `mode` (optional) is one of `replace` (the default) which means the
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   250
  loaded node will replace the current node content, `swap` to replace
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   251
  the current node with the loaded node, and `append` which will
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   252
  append the loaded node to the current node content
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   253
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   254
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   255
About the `callback` option:
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   256
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   257
* it is called with two parameters: the current node, and a list
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   258
  containing the loaded (and post-processed node)
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   259
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   260
* whenever is returns another function, this function is called in
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   261
  turn with the same parameters as above
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   262
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   263
This mecanism allows callback chaining.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   264
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   265
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   266
Javascript library: overview
3283
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   267
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   268
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   269
* jquery.* : jquery and jquery UI library
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   270
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   271
* cubicweb.ajax.js : concentrates all ajax related facilities (it
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   272
  extends jQuery with the loahxhtml function, provides a handfull of
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   273
  high-level ajaxy operations like asyncRemoteExec, reloadComponent,
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   274
  replacePageChunk, getDomFromResponse)
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   275
3283
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   276
* cubicweb.python.js : adds a number of practical extension to stdanrd
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   277
  javascript objects (on Date, Array, String, some list and dictionary
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   278
  operations), and a pythonesque way to build classes. Defines a
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   279
  CubicWeb namespace.
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   280
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   281
* cubicweb.htmlhelpers.js : a small bag of convenience functions used
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   282
  in various other cubicweb javascript resources (baseuri, progress
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   283
  cursor handling, popup login box, html2dom function, etc.)
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   284
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   285
* cubicweb.widgets.js : provides a widget namespace and constructors
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   286
  and helpers for various widgets (mainly facets and timeline)
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   287
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   288
* cubicweb.edition.js : used by edition forms
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   289
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   290
* cubicweb.preferences.js : used by the preference form
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   291
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   292
* cubicweb.facets.js : used by the facets mechanism
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   293
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   294
There is also javascript support for massmailing, gmap (google maps),
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   295
fckcwconfig (fck editor), timeline, calendar, goa (CubicWeb over
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   296
AppEngine), flot (charts drawing), tabs and bookmarks.