Skip to content

Commit 438ba77

Browse files
committed
[IMP] web: update javascript odoo modules
This commit adapts the javascript modules section with the last update odoo/odoo#142858 and some outdated features.
1 parent 603518a commit 438ba77

File tree

10 files changed

+47
-112
lines changed

10 files changed

+47
-112
lines changed

content/developer/howtos/javascript_client_action.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ framework and use services, core components, hooks,...
2525
.. code-block:: js
2626
:caption: :file:`my_client_action.js`
2727
28-
/** @odoo-module **/
29-
3028
import { registry } from "@web/core/registry";
3129
3230
import { Component } from "@odoo/owl";

content/developer/howtos/javascript_field.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ displaying "Late!" in red whenever the checkbox is checked.
1414
.. code-block:: javascript
1515
:caption: :file:`late_order_boolean_field.js`
1616
17-
/** @odoo-module */
18-
1917
import { registry } from "@web/core/registry";
2018
import { BooleanField } from "@web/views/fields/boolean/boolean_field";
2119
import { Component, xml } from "@odoo/owl";
@@ -63,8 +61,6 @@ Assume that we want to create a field that displays a simple text in red.
6361
.. code-block:: js
6462
:caption: :file:`my_text_field.js`
6563
66-
/** @odoo-module */
67-
6864
import { standardFieldProps } from "@web/views/fields/standard_field_props";
6965
import { Component, xml } from "@odoo/owl";
7066
import { registry } from "@web/core/registry";

content/developer/howtos/javascript_view.rst

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ can be done in a few steps:
1414
.. code-block:: js
1515
:caption: :file:`custom_kanban_controller.js`
1616
17-
/** @odoo-module */
18-
1917
import { KanbanController } from "@web/views/kanban/kanban_controller";
2018
import { kanbanView } from "@web/views/kanban/kanban_view";
2119
import { registry } from "@web/core/registry";
@@ -82,8 +80,6 @@ Creating a new view is an advanced topic. This guide highlight only the essentia
8280
.. code-block:: js
8381
:caption: :file:`beautiful_controller.js`
8482
85-
/** @odoo-module */
86-
8783
import { Layout } from "@web/search/layout";
8884
import { useService } from "@web/core/utils/hooks";
8985
import { Component, onWillStart, useState} from "@odoo/owl";
@@ -161,8 +157,6 @@ Creating a new view is an advanced topic. This guide highlight only the essentia
161157
.. code-block:: js
162158
:caption: :file:`beautiful_model.js`
163159
164-
/** @odoo-module */
165-
166160
import { KeepLast } from "@web/core/utils/concurrency";
167161
168162
export class BeautifulModel {
@@ -199,8 +193,6 @@ Creating a new view is an advanced topic. This guide highlight only the essentia
199193
.. code-block:: js
200194
:caption: :file:`beautiful_arch_parser.js`
201195
202-
/** @odoo-module */
203-
204196
import { XMLParser } from "@web/core/utils/xml";
205197
206198
export class BeautifulArchParser extends XMLParser {
@@ -219,8 +211,6 @@ Creating a new view is an advanced topic. This guide highlight only the essentia
219211
.. code-block:: js
220212
:caption: :file:`beautiful_view.js`
221213
222-
/** @odoo-module */
223-
224214
import { registry } from "@web/core/registry";
225215
import { BeautifulController } from "./beautiful_controller";
226216
import { BeautifulArchParser } from "./beautiful_arch_parser";

content/developer/howtos/standalone_owl_application.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ Then create the JavaScript file for that component in :file:`/your_module/static
3737

3838
.. code-block:: js
3939
40-
/** @odoo-module */
4140
import { Component } from "@odoo/owl";
4241
4342
export class Root extends Component {
@@ -50,7 +49,6 @@ file. Create the JavaScript file that will mount the app in :file:`/your_module/
5049

5150
.. code-block:: js
5251
53-
/** @odoo-module */
5452
import { whenReady } from "@odoo/owl";
5553
import { mountComponent } from "@web/env";
5654
import { Root } from "./root";

content/developer/howtos/website_themes/theming.rst

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -591,16 +591,6 @@ Odoo supports three different kinds of JavaScript files:
591591
Most new Odoo JavaScript codes should use the native JavaScript module system. It's simpler and
592592
brings the benefit of a better developer experience with better integration with the IDE.
593593

594-
.. important::
595-
Odoo needs to know which files should be translated into :ref:`Odoo modules
596-
<frontend/js_modules>` and which files should not. It's an opt-in system: Odoo looks at the first
597-
line of a JavaScript file and checks if it contains the string `@odoo-module`. If so,
598-
it will automatically be converted to an Odoo module.
599-
600-
.. code-block:: javascript
601-
602-
/** @odoo-module **/
603-
604594
**Declaration**
605595

606596
.. code-block:: python

content/developer/reference/backend/testing.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,6 @@ Javascript
585585

586586
.. code-block:: javascript
587587
588-
/** @odoo-module **/
589588
import tour from 'web_tour.tour';
590589
tour.register('rental_product_configurator_tour', {
591590
url: '/web', // Here, you can specify any other starting url

content/developer/reference/frontend/javascript_modules.rst

Lines changed: 47 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -51,50 +51,64 @@ execute them precisely in that order).
5151
Native Javascript Modules
5252
=========================
5353

54-
Most new Odoo javascript code should use the native javascript module system. This
55-
is simpler, and brings the benefits of a better developer experience with a better
56-
integration with the IDE.
57-
58-
There is a very important point to know: Odoo needs to know which files
59-
should be translated into :ref:`Odoo modules <frontend/modules/odoo_module>` and which
60-
files should not be translated. This is an opt-in system: Odoo will look at the
61-
first line of a JS file and check if it contains the string *@odoo-module*. If so, it will
62-
automatically be converted to an Odoo module.
54+
Odoo javascript code uses the native javascript module system. This is simpler, and
55+
brings the benefits of a better developer experience with a better integration with the IDE.
6356

64-
For example, let us consider the following module, located in :file:`web/static/src/file_a.js`:
57+
Let us consider the following module, located in :file:`web/static/src/file_a.js`:
6558

6659
.. code-block:: javascript
6760
68-
/** @odoo-module **/
69-
import { someFunction } from './file_b';
61+
import { someFunction } from "./file_b";
7062
7163
export function otherFunction(val) {
7264
return someFunction(val + 3);
7365
}
7466
75-
Note the comment in the first line: it describes that this file should be converted.
76-
Any file without this comment will be kept as-is (which will most likely be an
77-
error). This file will then be translated into an Odoo module that look like this:
67+
There is a very important point to know: by default Odoo transpiles files under
68+
`/static/src` and `/static/tests` into :ref:`Odoo modules <frontend/modules/odoo_module>`.
69+
This file will then be transpiled into an Odoo module that looks like this:
7870

7971
.. code-block:: javascript
8072
81-
odoo.define('@web/file_a', function (require) {
73+
odoo.define('@web/file_a', ['@web/file_b'], function (require) {
8274
'use strict';
8375
let __exports = {};
8476
8577
const { someFunction } = require("@web/file_b");
8678
8779
__exports.otherFunction = function otherFunction(val) {
88-
return someFunction(val + 3);
80+
return someFunction(val + 3);
8981
};
9082
9183
return __exports;
9284
)};
9385
94-
So, as you can see, the transformation is basically adding `odoo.define` on top,
95-
and updating the import/export statements.
86+
So, as you can see, the transformation is basically adding `odoo.define` on top
87+
and updating the import/export statements. This is an opt-out system, it's possible
88+
to tell the transpiler to ignore the file.
89+
90+
.. code-block:: javascript
91+
92+
/** @odoo-module ignore **/
93+
(function () {
94+
const sum = (a, b) => a + b;
95+
console.log(sum(1, 2));
96+
)();
97+
98+
Note the comment in the first line: it describes that this file should be ignored.
99+
100+
In other folders, files aren't transpiled by default, it is opt-in. Odoo will look at the
101+
first line of a JS file and check if it contains a comment with *@odoo-module*. If so, it will
102+
automatically be converted to an Odoo module.
103+
104+
.. code-block:: javascript
105+
106+
/** @odoo-module **/
107+
export function sum(a, b) {
108+
return a + b;
109+
}
96110
97-
Another important point is that the translated module has an official name:
111+
Another important point is that the transpiled module has an official name:
98112
*@web/file_a*. This is the actual name of the module. Every relative imports
99113
will be converted as well. Every file located in an Odoo addon
100114
:file:`some_addon/static/src/path/to/file.js` will be assigned a name prefixed by the
@@ -120,16 +134,13 @@ The file :file:`file_b` can import :file:`file_a` like this:
120134
121135
.. code-block:: javascript
122136
123-
/** @odoo-module **/
124-
import {something} from `./file_a`
137+
import {something} from `./file_a`;
125138
126139
But :file:`file_c` need to use the full name:
127140
128141
.. code-block:: javascript
129142
130-
/** @odoo-module **/
131-
import {something} from `@web/file_a`
132-
143+
import {something} from `@web/file_a`;
133144
134145
Aliased modules
135146
---------------
@@ -155,7 +166,7 @@ Then, the translated module will also create an alias with the requested name:
155166
156167
.. code-block:: javascript
157168
158-
odoo.define(`web.someName`, function(require) {
169+
odoo.define(`web.someName`, ['@web/file_a'], function(require) {
159170
return require('@web/file_a')[Symbol.for("default")];
160171
});
161172
@@ -180,7 +191,7 @@ original module:
180191
181192
.. code-block:: javascript
182193
183-
odoo.define(`web.someName`, function(require) {
194+
odoo.define(`web.someName`, ["@web/file_a"], function(require) {
184195
return require('@web/file_a');
185196
});
186197
@@ -279,7 +290,7 @@ As an example, it may look like this:
279290
.. code-block:: javascript
280291
281292
// in file a.js
282-
odoo.define('module.A', function (require) {
293+
odoo.define('module.A', [], function (require) {
283294
"use strict";
284295
285296
var A = ...;
@@ -288,7 +299,7 @@ As an example, it may look like this:
288299
});
289300
290301
// in file b.js
291-
odoo.define('module.B', function (require) {
302+
odoo.define('module.B', ['module.A'], function (require) {
292303
"use strict";
293304
294305
var A = require('module.A');
@@ -298,20 +309,6 @@ As an example, it may look like this:
298309
return B;
299310
});
300311
301-
An alternative way to define a module is to give explicitly a list of dependencies
302-
in the second argument.
303-
304-
.. code-block:: javascript
305-
306-
odoo.define('module.Something', ['module.A', 'module.B'], function (require) {
307-
"use strict";
308-
309-
var A = require('module.A');
310-
var B = require('module.B');
311-
312-
// some code
313-
});
314-
315312
316313
If some dependencies are missing/non ready, then the module will simply not be
317314
loaded. There will be a warning in the console after a few seconds.
@@ -332,12 +329,13 @@ The `odoo.define` method is given three arguments:
332329
If the name is not unique, an exception will be thrown and displayed in the
333330
console.
334331

335-
- `dependencies`: the second argument is optional. If given, it should be a list
336-
of strings, each corresponding to a javascript module. This describes the
337-
dependencies that are required to be loaded before the module is executed. If
338-
the dependencies are not explicitly given here, then the module system will
339-
extract them from the function by calling toString on it, then using a regexp
340-
to find all the `require` statements.
332+
- `dependencies`: It should be a list of strings, each corresponding to a
333+
javascript module. This describes the dependencies that are required to
334+
be loaded before the module is executed.
335+
336+
- finally, the last argument is a function which defines the module. Its return
337+
value is the value of the module, which may be passed to other modules requiring
338+
it.
341339

342340
.. code-block:: javascript
343341
@@ -350,11 +348,6 @@ The `odoo.define` method is given three arguments:
350348
return something;
351349
});
352350
353-
- finally, the last argument is a function which defines the module. Its return
354-
value is the value of the module, which may be passed to other modules requiring
355-
it. Note that there is a small exception for asynchronous modules, see the
356-
next section.
357-
358351
If an error happens, it will be logged (in debug mode) in the console:
359352

360353
* `Missing dependencies`:
@@ -369,24 +362,3 @@ If an error happens, it will be logged (in debug mode) in the console:
369362
Modules who depend on a rejected module
370363
* `Non loaded modules`:
371364
Modules who depend on a missing or a failed module
372-
373-
Asynchronous modules
374-
--------------------
375-
376-
It can happen that a module needs to perform some work before it is ready. For
377-
example, it could do an rpc to load some data. In that case, the module can
378-
simply return a promise. The module system will simply
379-
wait for the promise to complete before registering the module.
380-
381-
.. code-block:: javascript
382-
383-
odoo.define('module.Something', function (require) {
384-
"use strict";
385-
386-
var ajax = require('web.ajax');
387-
388-
return ajax.rpc(...).then(function (result) {
389-
// some code here
390-
return something;
391-
});
392-
});

content/developer/reference/frontend/services.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,6 @@ Let's say we want to add an effect that add a sepia look at the page.
312312

313313
.. code-block:: javascript
314314
315-
/** @odoo-module **/
316-
317315
import { registry } from "@web/core/registry";
318316
const { Component, tags } = owl;
319317

content/developer/tutorials/discover_js_framework/01_owl_components.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,6 @@ see how to create a `sub-component <{OWL_PATH}/doc/reference/component.md#sub-co
120120
as the component. For example, if we have a `TodoList` component, its code should be in
121121
`todo_list.js`, `todo_list.xml` and if necessary, `todo_list.scss`
122122

123-
.. important::
124-
Don't forget :code:`/** @odoo-module **/` in your JavaScript files. More information on this can
125-
be found :ref:`here <frontend/modules/native_js>`.
126-
127123
.. _tutorials/discover_js_framework/simple_card:
128124

129125
3. A simple `Card` component

content/developer/tutorials/master_odoo_web_framework/02_create_gallery_view.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,6 @@ is hardcoded in the controller.
209209

210210
.. code-block:: js
211211
212-
/** @odoo-module */
213-
214212
import { registry } from '@web/core/registry';
215213
import { galleryView } from '@awesome_gallery/gallery_view';
216214
import { GalleryRenderer } from '@awesome_gallery/gallery_renderer';

0 commit comments

Comments
 (0)