Introduction to the Odoo JavaScript framework
Beginner
Almost every app you create in Odoo will need some JavaScript to do what you like and the options with the JavaScript framework are almost unlimited. You can override existing features, expand features or create your own new view types for example!
This tutorial is the first in a series of tutorials that will cover many aspects of the Odoo JavaScript framework. It is interesting that as the Odoo ecosystem is getting bigger, it is still difficult to find some quality information on the Javascript part of Odoo but there are some reasons for this:
Nevertheless the JS framework is quite often still the only solution to problems. As the JS framework is a large subject each tutorial will focus on one task. In this series of tutorials you will learn about quite some features from the JS framework:
Broadly speaking the JS framework can be split up into a few parts:
Each of these parts of Odoo talk to the same server, written in Python, with a postgreSQL database. Speaking of the database, Odoo has made a very interesting architectural choice: many templates are stored in the database as an XML document. This is very powerful, since this allows any installed addon to be modified with a simple xpath expression. This is certainly the most important reason that the Odoo web client UI is so dynamic and extensible.
This series of tutorial will focus mostly on the web client, but many of the topics discussed will also apply to the website or the point of sale JS code.
Enough talked, lets start coding! We'll need an own module to add our files and code into. It is a good practice to separate custom addons in a different folder, so it is easier to maintain. Let us start by creating a "tutorial_add_javascript" folder with two files in our custom addons folder. Create a new "__init__.py" and "__manifest__.py" file in this new folder.
This is the minimal amount of files required by Odoo. The "__manifest__.py" should contain a description of how the module is structured, and __init__.py makes it a python module. For now we can leave the "__init__.py" file empty. Open "__manifest__.py" and add the following values:
{ 'name': "tutorial_add_javascript", 'summary': """ Example Hello World application""", 'description': """ This is an example application to learn the basics of the JS framework. """, 'author': "An Awesome Developer", 'version': '13.0.0.1', 'depends': ['base', 'web'], 'application': True,}
Note that we set the application flag to true, to make it a real application, not just a technical addon. Another interesting fact is that we added web to its list of dependencies: this means that installing "tutorial_add_javascript" will force Odoo to install the web client as well. That's is, we now have a working addon! It does nothing, but still, it is there! If your Odoo server is properly configured/started (meaning that the "tutorial_add_javascript" parent folder is available on the addons path), then the app can now be installed. Notice that you'll need to restart your Odoo instance and update the apps list before it shows up.
So, what's next? We now have a module that does nothing so let's add our very first JavaScript file! The Odoo server has a special folder which is served to the outside world: "static". All files that are required for the web browser should live in static. Note that there is a convention in Odoo to separate the files in an hierarchy looking like this:
your_module_name/ static/ src/ js/ some_file.js some_other_file.js css/ some_file.css xml/ other_file.xml ...
Create the folder "static" and in this folder add "src" then "js" and create a new file named "hello_world.js" in the "js" folder. Your module should now have this structure:
tutorial_add_javascript/ static/ src/ js/ hello_world.js
Open the "hello_world.js" file and add a simple console.log in it:
console.log('Hello Javascript')
If you now update/install the module you can open the following URL:
http://localhost:8069/tutorial_add_javascript/static/src/js/hello_world.js
If all goes well, you should see your JavaScript file in the browser! This means that it is properly served by Odoo. Of course, it is cheating. The JavaScript code is not currently being executed. To do that, we need to actually load our file with the Odoo assets.
The next step is to tell Odoo that our "hello_world.js" file should be loaded along with all the other JavaScript files in the web client. To do this we have to add our JavaScript file to the assets. Create a new directory "views" under "your_module_name/" and in this directory create a new file named "assets.xml". Have a look at this code first, I'll explain it afterwards:
So, does this code make sense to you? This will tell Odoo that we want to inherit the XML template "assets_backend" - which contains all backend JavaScript resources - and that we want to add some file(s) to the assets. The file is of the type "text/javascript" and is in our module at the path "/tutorial_add_javascript/static/src/js/hello_world.js". This is enough for the Odoo framework to know the location of our custom JavaScript file and to add it to the assets. We'll go into more details about this in the next tutorial but for now this is enough.
The next step is to ask the Odoo server to read this XML file so that this XML gets loaded into Odoo too. You can do this by adding the XML file its location in the "__manifest__.py" file in Odoo like this:
'data': [ 'views/assets.xml', ],
Now that we've modified the "__manifest__.py" file we need to restart the server and update our "tutorial_add_javascript" module so that Odoo rereads the file. Finally, open Odoo in your browser and open the JavaScript console (usually by pressing F12 or ctrl+shift+i) or by using the developer tools menu. You should now see the welcome message coming from your JavaScript file:
In this tutorial you've learned where Odoo locates its static files and how to add custom JavaScript files to Odoo. This tutorial helps you understand the basics of the JavaScript framework and how to load files. This is enough to be able to do some kind of customizations but it clearly doesn't interact with the Odoo web client its JavaScript code though. In the following tutorial we will explain how to create your own JavaScript functions and how to create client actions.