[MERGE] callback2deferred session.rpc
[odoo/odoo.git] / doc / guides / client-action.rst
1 .. highlight:: javascript
2
3 Creating a new client action
4 ============================
5
6 Client actions are the client-side version of OpenERP's "Server
7 Actions": instead of allowing for semi-arbitrary code to be executed
8 in the server, they allow for execution of client-customized code.
9
10 On the server side, a client action is an action of type
11 ``ir.actions.client``, which has (at most) two properties: a mandatory
12 ``tag``, which is an arbitrary string by which the client will
13 identify the action, and an optional ``params`` which is simply a map
14 of keys and values sent to the client as-is (this way, client actions
15 can be made generic and reused in multiple contexts).
16
17 General Structure
18 -----------------
19
20 In the OpenERP Web code, a client action only requires two pieces of
21 information:
22
23 * Mapping the action's ``tag`` to an object
24
25 * Providing said object. Two different types of objects can be mapped
26   to a client action:
27
28   * An OpenERP Web widget, which must inherit from
29     :js:class:`openerp.web.Widget`
30
31   * A regular javascript function
32
33 The major difference is in the lifecycle of these:
34
35 * if the client action maps to a function, the function will simply be
36   called when executing the action. The function can have no further
37   interaction with the Web Client itself, although it can return an
38   action which will be executed after it.
39
40 * if, on the other hand, the client action maps to a
41   :js:class:`~openerp.web.Widget`, that
42   :js:class:`~openerp.web.Widget` will be instantiated and added to
43   the web client's canvas, with the usual
44   :js:class:`~openerp.web.Widget` lifecycle (essentially, it will
45   either take over the content area of the client or it will be
46   integrated within a dialog).
47
48 For example, to create a client action displaying a ``res.widget``
49 object::
50
51     // Registers the object 'openerp.web_dashboard.Widget' to the client
52     // action tag 'board.home.widgets'
53     instance.web.client_actions.add(
54         'board.home.widgets', 'openerp.web_dashboard.Widget');
55     instance.web_dashboard.Widget = instance.web.Widget.extend({
56         template: 'HomeWidget'
57     });
58
59 At this point, the generic :js:class:`~openerp.web.Widget` lifecycle
60 takes over, the template is rendered, inserted in the client DOM,
61 bound on the object's ``$el`` property and the object is started.
62
63 If the client action takes parameters, these parameters are passed in as a
64 second positional parameter to the constructor::
65
66     init: function (parent, params) {
67         // execute the Widget's init
68         this._super(parent);
69         // board.home.widgets only takes a single param, the identifier of the
70         // res.widget object it should display. Store it for later
71         this.widget_id = params.widget_id;
72     }
73
74 More complex initialization (DOM manipulations, RPC requests, ...)
75 should be performed in the :js:func:`~openerp.web.Widget.start()`
76 method.
77
78 .. note::
79
80     As required by :js:class:`~openerp.web.Widget`'s contract, if
81     :js:func:`~openerp.web.Widget.start()` executes any asynchronous
82     code it should return a ``$.Deferred`` so callers know when it's
83     ready for interaction.
84
85     Although generally speaking client actions are not really
86     interacted with.
87
88 .. code-block:: javascript
89
90     start: function () {
91         return $.when(
92             this._super(),
93             // Simply read the res.widget object this action should display
94             new instance.web.Model('res.widget').call(
95                 'read', [[this.widget_id], ['title']])
96                 .then(this.proxy('on_widget_loaded'));
97     }
98
99 The client action can then behave exactly as it wishes to within its
100 root (``this.$el``). In this case, it performs further renderings once
101 its widget's content is retrieved::
102
103     on_widget_loaded: function (widgets) {
104         var widget = widgets[0];
105         var url = _.sprintf(
106             '/web_dashboard/widgets/content?session_id=%s&widget_id=%d',
107             this.session.session_id, widget.id);
108         this.$el.html(QWeb.render('HomeWidget.content', {
109             widget: widget,
110             url: url
111         }));
112     }