@@ -51,50 +51,64 @@ execute them precisely in that order).
5151Native 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
99113will 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
126139But : 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
134145Aliased 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
317314loaded. 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- });
0 commit comments