doc/book/devweb/js.rst
author Nicolas Chauvat <nicolas.chauvat@logilab.fr>
Sat, 15 Feb 2020 17:08:15 +0100
changeset 12879 7347715bf0ee
parent 10495 5bd914ebf3ae
permissions -rw-r--r--
[doc] replace tabs with spaces (and make linter happy)
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
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
     7
ships with jquery (1.3.x) and parts of the jquery UI library, plus a
5186
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5157
diff changeset
     8
number of homegrown files and also other third party libraries.
3283
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
     9
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    10
All javascript files are stored in cubicweb/web/data/. There are
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    11
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
    12
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    13
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
    14
once, hence the framework provides a number of mechanisms and
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    15
conventions to deal with javascript resources.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    16
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    17
Conventions
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    18
~~~~~~~~~~~
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    19
3283
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    20
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
    21
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
    22
5400
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    23
.. XXX external_resources variable (which needs love)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    24
5475
b44bad36e609 [doc/book] refresh a bit the edit controller section
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5400
diff changeset
    25
Server-side Javascript API
b44bad36e609 [doc/book] refresh a bit the edit controller section
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5400
diff changeset
    26
~~~~~~~~~~~~~~~~~~~~~~~~~~
3283
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    27
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    28
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
    29
request object (available as self._cw from most application objects,
3283
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    30
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
    31
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    32
* `add_js(self, jsfiles, localfile=True)` which takes a sequence of
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    33
  javascript files and writes proper entries into the HTML header
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    34
  section. The localfile parameter allows to declare resources which
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    35
  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
    36
  network).
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    37
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    38
* `add_onload(self, jscode)` which adds one raw javascript code
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    39
  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
    40
  up early jQuery(document).ready(...) initialisations.
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
    41
5475
b44bad36e609 [doc/book] refresh a bit the edit controller section
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5400
diff changeset
    42
Javascript events
b44bad36e609 [doc/book] refresh a bit the edit controller section
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5400
diff changeset
    43
~~~~~~~~~~~~~~~~~
4830
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
* ``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
    46
  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
    47
  to callbacks :
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    48
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    49
  - ``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
    50
    ajax request
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    51
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    52
  - ``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
    53
    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
    54
    requests.
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    55
5475
b44bad36e609 [doc/book] refresh a bit the edit controller section
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5400
diff changeset
    56
Important javascript AJAX APIS
b44bad36e609 [doc/book] refresh a bit the edit controller section
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5400
diff changeset
    57
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4830
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4437
diff changeset
    58
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
    59
* `asyncRemoteExec` and `remoteExec` are the base building blocks for
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
    60
  doing arbitrary async (resp. sync) communications with the server
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
    61
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
    62
* `reloadComponent` is a convenience function to replace a DOM node
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
    63
  with server supplied content coming from a specific registry (this
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
    64
  is quite handy to refresh the content of some boxes for instances)
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
    65
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    66
* `jQuery.fn.loadxhtml` is an important extension to jQuery which
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
    67
  allows proper loading and in-place DOM update of xhtml views. It is
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    68
  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
    69
  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
    70
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    71
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    72
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
    73
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    74
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
    75
On the python side, we have to define an
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
    76
:class:`cubicweb.web.views.ajaxcontroller.AjaxFunction` object. The
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
    77
simplest way to do that is to use the
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
    78
:func:`cubicweb.web.views.ajaxcontroller.ajaxfunc` decorator (for more
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
    79
details on this, refer to :ref:`ajax`).
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    80
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    81
.. sourcecode: python
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    82
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
    83
    from cubicweb.web.views.ajaxcontroller import ajaxfunc
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    84
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
    85
    # serialize output to json to get it back easily on the javascript side
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
    86
    @ajaxfunc(output_type='json')
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    87
    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
    88
        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
    89
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
    90
On the javascript side, we do the asynchronous call. Notice how it
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    91
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
    92
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
    93
methods.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    94
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    95
.. sourcecode: javascript
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    96
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
    97
    function asyncHello(name) {
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
    98
        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
    99
        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
   100
            alert(response);
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   101
        });
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   102
        deferred.addErrback(function (error) {
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   103
            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
   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
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   107
     function syncHello(name) {
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   108
         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
   109
     }
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   110
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   111
Anatomy of a reloadComponent call
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   112
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   113
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   114
`reloadComponent` allows to dynamically replace some DOM node with new
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   115
elements. It has the following signature:
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   116
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   117
* `compid` (mandatory) is the name of the component to be reloaded
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   118
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   119
* `rql` (optional) will be used to generate a result set given as
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   120
  argument to the selected component
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   121
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   122
* `registry` (optional) defaults to 'components' but can be any other
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   123
  valid registry name
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   124
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   125
* `nodeid` (optional) defaults to compid + 'Component' but can be any
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   126
  explicitly specified DOM node id
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   127
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   128
* `extraargs` (optional) should be a dictionary of values that will be
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   129
  given to the cell_call method of the component
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   130
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   131
A simple reloadComponent example
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   132
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   133
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   134
The server side implementation of `reloadComponent` is the
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
   135
:func:`cubicweb.web.views.ajaxcontroller.component` *AjaxFunction* appobject.
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   136
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   137
The following function implements a two-steps method to delete a
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   138
standard bookmark and refresh the UI, while keeping the UI responsive.
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   139
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   140
.. sourcecode:: javascript
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   141
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   142
    function removeBookmark(beid) {
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   143
        d = asyncRemoteExec('delete_bookmark', beid);
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   144
        d.addCallback(function(boxcontent) {
12879
7347715bf0ee [doc] replace tabs with spaces (and make linter happy)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 10495
diff changeset
   145
            reloadComponent('bookmarks_box', '', 'boxes', 'bookmarks_box');
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   146
            document.location.hash = '#header';
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   147
            updateMessage(_("bookmark has been removed"));
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   148
         });
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   149
    }
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   150
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   151
`reloadComponent` is called with the id of the bookmark box as
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   152
argument, no rql expression (because the bookmarks display is actually
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   153
independant of any dataset context), a reference to the 'boxes'
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   154
registry (which hosts all left, right and contextual boxes) and
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   155
finally an explicit 'bookmarks_box' nodeid argument that stipulates
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   156
the target DOM node.
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   158
Anatomy of a loadxhtml call
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   159
~~~~~~~~~~~~~~~~~~~~~~~~~~~
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   160
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   161
`jQuery.fn.loadxhtml` is an important extension to jQuery which allows
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   162
proper loading and in-place DOM update of xhtml views. The existing
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   163
`jQuery.load`_ function does not handle xhtml, hence the addition. The
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   164
API of loadxhtml is roughly similar to that of `jQuery.load`_.
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   165
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   166
.. _`jQuery.load`: http://api.jquery.com/load/
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   167
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   168
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   169
* `url` (mandatory) should be a complete url (typically referencing
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
   170
  the :class:`cubicweb.web.views.ajaxcontroller.AjaxController`,
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
   171
  but this is not strictly mandatory)
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   172
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   173
* `data` (optional) is a dictionary of values given to the
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   174
  controller specified through an `url` argument; some keys may have a
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   175
  special meaning depending on the choosen controller (such as `fname`
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   176
  for the JSonController); the `callback` key, if present, must refer
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   177
  to a function to be called at the end of loadxhtml (more on this
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   178
  below)
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   179
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   180
* `reqtype` (optional) specifies the request method to be used (get or
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   181
  post); if the argument is 'post', then the post method is used,
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   182
  otherwise the get method is used
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   183
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   184
* `mode` (optional) is one of `replace` (the default) which means the
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   185
  loaded node will replace the current node content, `swap` to replace
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   186
  the current node with the loaded node, and `append` which will
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   187
  append the loaded node to the current node content
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   188
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   189
About the `callback` option:
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   190
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   191
* it is called with two parameters: the current node, and a list
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   192
  containing the loaded (and post-processed node)
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   193
8987
d9195dce3a5b [doc] a couple almost-typos
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8128
diff changeset
   194
* whenever it returns another function, this function is called in
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   195
  turn with the same parameters as above
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   196
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   197
This mechanism allows callback chaining.
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   198
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   199
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   200
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
   201
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   202
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   203
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
   204
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
   205
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
   206
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   207
.. sourcecode:: python
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   208
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
   209
    from cubicweb.web.views.ajaxcontroller import ajaxfunc
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   210
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
   211
    @ajaxfunc(output_type='xhtml')
8987
d9195dce3a5b [doc] a couple almost-typos
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8128
diff changeset
   212
    def frob_status(self, eid, frobname):
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
   213
        entity = self._cw.entity_from_eid(eid)
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   214
        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
   215
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   216
.. sourcecode:: javascript
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   217
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
   218
    function updateSomeDiv(divid, eid, frobname) {
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   219
        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
   220
        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
   221
     }
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   222
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   223
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
   224
instance (it should contain something like
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
   225
`http://myinstance/ajax?`). The actual AjaxController method name is
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   226
encoded in the `params` dictionary using the `fname` key.
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   227
5475
b44bad36e609 [doc/book] refresh a bit the edit controller section
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5400
diff changeset
   228
A more real-life example
b44bad36e609 [doc/book] refresh a bit the edit controller section
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5400
diff changeset
   229
~~~~~~~~~~~~~~~~~~~~~~~~
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   230
5475
b44bad36e609 [doc/book] refresh a bit the edit controller section
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5400
diff changeset
   231
A frequent need of Web 2 applications is the delayed (or demand
b44bad36e609 [doc/book] refresh a bit the edit controller section
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5400
diff changeset
   232
driven) loading of pieces of the DOM. This is typically achieved using
b44bad36e609 [doc/book] refresh a bit the edit controller section
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5400
diff changeset
   233
some preparation of the initial DOM nodes, jQuery event handling and
b44bad36e609 [doc/book] refresh a bit the edit controller section
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5400
diff changeset
   234
proper use of loadxhtml.
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   235
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   236
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
   237
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
   238
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   239
.. sourcecode:: python
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   240
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   241
    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
   242
        """ 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
   243
        w = self.w
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   244
        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
   245
        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
   246
        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
   247
            urlparams['rql'] = rql
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   248
        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
   249
            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
   250
        w(u'</div>')
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   251
        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
   252
            jQuery('#lazy-%(vid)s').bind('%(event)s', function() {
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
   253
                   loadNow('#lazy-%(vid)s');});"""
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   254
            % {'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
   255
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   256
This creates a `div` with a specific event associated to it.
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   257
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   258
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
   259
5152
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5151
diff changeset
   260
* optional parameters such as an entity eid, an rset
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   261
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   262
* 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
   263
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   264
* 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
   265
  not loaded
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   266
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   267
* 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
   268
  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
   269
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   270
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
   271
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   272
.. sourcecode:: javascript
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   273
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
   274
    function loadNow(eltsel) {
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   275
        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
   276
        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
   277
    }
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   278
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   279
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
   280
(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
   281
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   282
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
   283
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
   284
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
   285
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
   286
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
   287
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
   288
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
   289
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   290
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
   291
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
   292
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
   293
ungodly hacks.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   294
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   295
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
   296
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
   297
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
   298
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
   299
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   300
On the server side, a simple call to a javascript function is
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   301
sufficient.
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   302
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   303
.. sourcecode:: python
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   304
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   305
    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
   306
        """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
   307
        on dom readyness
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   308
        """
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
   309
        self._cw.add_onload("triggerLoad('%s');" % vid)
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   310
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   311
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
   312
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
   313
.. sourcecode:: javascript
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   314
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
   315
    function triggerLoad(divid) {
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   316
        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
   317
    }
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   318
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   319
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   320
Javascript library: overview
3283
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   321
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   322
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   323
* jquery.* : jquery and jquery UI library
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   324
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   325
* 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
   326
  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
   327
  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
   328
  replacePageChunk, getDomFromResponse)
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   329
3283
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   330
* cubicweb.python.js : adds a number of practical extension to stdanrd
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   331
  javascript objects (on Date, Array, String, some list and dictionary
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   332
  operations), and a pythonesque way to build classes. Defines a
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   333
  CubicWeb namespace.
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   334
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   335
* cubicweb.htmlhelpers.js : a small bag of convenience functions used
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   336
  in various other cubicweb javascript resources (baseuri, progress
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   337
  cursor handling, popup login box, html2dom function, etc.)
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   338
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   339
* cubicweb.widgets.js : provides a widget namespace and constructors
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   340
  and helpers for various widgets (mainly facets and timeline)
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   341
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   342
* cubicweb.edition.js : used by edition forms
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   343
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   344
* cubicweb.preferences.js : used by the preference form
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   345
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   346
* cubicweb.facets.js : used by the facets mechanism
4f53eb3f1331 more doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1714
diff changeset
   347
5151
2f70fa8b6854 [doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   348
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
   349
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
   350
AppEngine), flot (charts drawing), tabs and bookmarks.
5470
fb004819cab4 [book] add generated js documentation to cw book
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 5400
diff changeset
   351
fb004819cab4 [book] add generated js documentation to cw book
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 5400
diff changeset
   352
API
fb004819cab4 [book] add generated js documentation to cw book
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 5400
diff changeset
   353
~~~
fb004819cab4 [book] add generated js documentation to cw book
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 5400
diff changeset
   354
fb004819cab4 [book] add generated js documentation to cw book
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 5400
diff changeset
   355
.. toctree::
fb004819cab4 [book] add generated js documentation to cw book
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 5400
diff changeset
   356
    :maxdepth: 1
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
   357
10495
5bd914ebf3ae [doc] fix warnings/errors in doc build
Julien Cristau <julien.cristau@logilab.fr>
parents: 10491
diff changeset
   358
    /js_api/index
5742
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   359
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   360
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   361
Testing javascript
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5742
diff changeset
   362
~~~~~~~~~~~~~~~~~~
5742
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   363
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   364
You with the ``cubicweb.qunit.QUnitTestCase`` can include standard Qunit tests
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   365
inside the python unittest run . You simply have to define a new class that
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   366
inherit from ``QUnitTestCase`` and register your javascript test file in the
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   367
``all_js_tests`` lclass attribut. This  ``all_js_tests`` is a sequence a
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   368
3-tuple (<test_file, [<dependencies> ,] [<data_files>]):
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   369
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   370
The <test_file> should contains the qunit test. <dependencies> defines the list
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   371
of javascript file that must be imported before the test script.  Dependencies
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   372
are included their definition order. <data_files> are additional files copied in the
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   373
test directory. both <dependencies> and <data_files> are optionnal.
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   374
``jquery.js`` is preincluded in for all test.
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   375
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   376
.. sourcecode:: python
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   377
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   378
    from cubicweb.qunit import QUnitTestCase
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   379
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   380
    class MyQUnitTest(QUnitTestCase):
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   381
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   382
        all_js_tests = (
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   383
            ("relative/path/to/my_simple_testcase.js",)
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   384
            ("relative/path/to/my_qunit_testcase.js",(
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   385
                "rel/path/to/dependency_1.js",
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   386
                "rel/path/to/dependency_2.js",)),
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   387
            ("relative/path/to/my_complexe_qunit_testcase.js",(
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   388
                 "rel/path/to/dependency_1.js",
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   389
                 "rel/path/to/dependency_2.js",
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   390
               ),(
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   391
                 "rel/path/file_dependency.html",
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   392
                 "path/file_dependency.json")
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   393
                ),
74c19dac29cf Add a QUnitTestCase class to run qunit test case.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5486
diff changeset
   394
            )