Skip to Content
Odoo Meniu
  • Autentificare
  • Try it free
  • Aplicații
    Finanțe
    • Contabilitate
    • Facturare
    • Cheltuieli
    • Spreadsheet (BI)
    • Documente
    • Semn
    Vânzări
    • CRM
    • Vânzări
    • POS Shop
    • POS Restaurant
    • Abonamente
    • Închiriere
    Site-uri web
    • Constructor de site-uri
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Lanț Aprovizionare
    • Inventar
    • Producție
    • PLM
    • Achiziție
    • Maintenance
    • Calitate
    Resurse Umane
    • Angajați
    • Recrutare
    • Time Off
    • Evaluări
    • Referințe
    • Flotă
    Marketing
    • Social Marketing
    • Marketing prin email
    • SMS Marketing
    • Evenimente
    • Automatizare marketing
    • Sondaje
    Servicii
    • Proiect
    • Foi de pontaj
    • Servicii de teren
    • Centru de asistență
    • Planificare
    • Programări
    Productivitate
    • Discuss
    • Aprobări
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Aplicații Terțe Odoo Studio Platforma Odoo Cloud
  • Industrii
    Retail
    • Book Store
    • Magazin de îmbrăcăminte
    • Magazin de Mobilă
    • Magazin alimentar
    • Magazin de materiale de construcții
    • Magazin de jucării
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Distribuitor de băuturi
    • Hotel
    Proprietate imobiliara
    • Real Estate Agency
    • Firmă de Arhitectură
    • Construcție
    • Estate Managament
    • Grădinărit
    • Asociația Proprietarilor de Proprietăți
    Consultanta
    • Firma de Contabilitate
    • Partener Odoo
    • Agenție de marketing
    • Law firm
    • Atragere de talente
    • Audit & Certification
    Producție
    • Textil
    • Metal
    • Mobilier
    • Mâncare
    • Brewery
    • Cadouri corporate
    Health & Fitness
    • Club Sportiv
    • Magazin de ochelari
    • Centru de Fitness
    • Wellness Practitioners
    • Farmacie
    • Salon de coafură
    Trades
    • Handyman
    • IT Hardware and Support
    • Asigurare socială de stat
    • Cizmar
    • Servicii de curățenie
    • HVAC Services
    Altele
    • Organizație nonprofit
    • Agenție de Mediu
    • Închiriere panouri publicitare
    • Fotografie
    • Închiriere biciclete
    • Asigurare socială
    Browse all Industries
  • Comunitate
    Învăță
    • Tutorials
    • Documentație
    • Certificări
    • Instruire
    • Blog
    • Podcast
    Empower Education
    • Program Educațional
    • Scale Up! Business Game
    • Visit Odoo
    Obține Software-ul
    • Descărcare
    • Compară Edițiile
    • Lansări
    Colaborați
    • Github
    • Forum
    • Evenimente
    • Translations
    • Devino Partener
    • Services for Partners
    • Înregistrează-ți Firma de Contabilitate
    Obține Servicii
    • Găsește un Partener
    • Găsiți un contabil
    • Meet an advisor
    • Servicii de Implementare
    • Referințe ale clienților
    • Suport
    • Actualizări
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Obține un demo
  • Prețuri
  • Ajutor

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Contabilitate
  • Inventar
  • PoS
  • Proiect
  • MRP
All apps
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
All Posts Oameni Insigne
Etichete (View all)
odoo accounting v14 pos v15
Despre acest forum
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
All Posts Oameni Insigne
Etichete (View all)
odoo accounting v14 pos v15
Despre acest forum
Suport

How to load JavaScript files from custom payment module only on /shop/payment page in Odoo 18

Abonare

Primiți o notificare când există activitate la acestă postare

Această întrebare a fost marcată
javascriptwebsite_saleWebsite
1 Răspunde
1633 Vizualizări
Imagine profil
Diego Naranjo

I'm developing a payment gateway module for Odoo 18. The module includes several JavaScript files that are currently loaded on all frontend pages, but these files are only used on the payment page /shop/payment.

What is the best practice in Odoo 18 to load JavaScript files conditionally, only when the user accesses the /shop/payment page?


Context:

Some data from current manifest.py:

'data': [

    'security/ir.model.access.csv',

    'views/payment_form_templates.xml',

],

'assets': {

'web.assets_frontend': [

    'cards_gateway/static/src/js/payment_form.js',

    'cards_gateway/static/src/scss/payment_form.scss',

],

Current payment_form.js structure:

/** @odoo-module **/


import { rpc } from '@web/core/network/rpc';

import publicWidget from '@web/legacy/js/public/public_widget';


publicWidget.registry.PaymentForm.include({

    events: Object.assign({}, publicWidget.registry.PaymentForm.prototype.events, {

        'change select[name="o_cards_brand"]': '_onCardBrandChange',

        'change select[name="o_cards_installments"]': '_onInstallmentsChange'

    }),


    /**

     * @override

     */

    async start() {

        await this._super(...arguments);

        this.cardsState = {

            amount: undefined,

            providerId: undefined,

            currency: undefined,

            selectedCardBrand: undefined,

            selectedInstallments: undefined,

            interestRate: 0.0

        };

        if (this.paymentContext) {

            this.cardsState.amount = this.paymentContext.amount;

            this.cardsState.providerId = this.paymentContext.providerId;

            this.cardsState.currency = this.paymentContext.currencyId;

        }

    },


All others functions....



});

export default publicWidget.registry.PaymentForm;

Goals:

  • Avoid loading JS files unnecessarily on all pages.
  • Maintain compatibility with Odoo 18's assets and module system.
  • Follow best practices recommended by Odoo SA and OCA.
  • Make the solution scalable for other similar modules.
  • What approach do you recommend to achieve conditional loading of JavaScript files in Odoo 18?

Thanks in advance.


0
Imagine profil
Abandonează
Imagine profil
Diego Naranjo
Autor Cel mai bun răspuns

I have found the solution, I share it:

Conditional Loading with a Dedicated Helper File

We implemented a strategy using Odoo's asset loading capabilities:

Conditional Execution in payment_form.js:

The main payment_form.js (a lightweight entry point in web.assets_frontend) now checks the current page. If it's /shop/payment, it dynamically loads a heavier helper file:

const isInPaymentPage = window.location.pathname.includes('/shop/payment');

if (isInPaymentPage) {

    loadJS("/cards_gateway/static/src/js/payment_form_helper.js").then(() => {

        // ... then extend publicWidget.registry.PaymentForm

    });

}

Dedicated Helper File (payment_form_helper.js):

All specific, heavier logic (e.g., currency formatting, error handling, transaction preparation, and detailed UI updates) was moved here.

Global Exposure: Functions are in an object (cardsPaymentHelpers) exposed to window for accessibility by payment_form.js.

Dependency Passing: rpc and _t (translation) are passed as arguments to helper functions, maintaining modularity without ES6 imports in the helper.

Benefits:

Reduced Initial Load: Lighter payment_form.js loads everywhere; heavy logic is deferred.

Improved Performance: Faster page loads on non-payment pages.


Clear Separation of Concerns: Clean orchestration in payment_form.js, detailed logic in payment_form_helper.js.

Scalability & Compatibility: Easily adaptable for other page-specific JS, leveraging standard Odoo features while optimizing.



0
Imagine profil
Abandonează
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Înscrie-te
Related Posts Răspunsuri Vizualizări Activitate
how to inherit _onCarrierUpdateAnswer() to update some info on selecting delivery carrier
javascript website_sale odoo11community
Imagine profil
Imagine profil
1
ian. 19
4294
[v15] header: change selector for cart popup Rezolvat
javascript inheritance override website_sale
Imagine profil
Imagine profil
2
dec. 22
3534
Cannot import @website_sale/js/utils
javascript
Imagine profil
Imagine profil
2
nov. 25
624
Cómo cerrar una transferencia interna al recibirla desde la vista de código de barras stock.picking
javascript
Imagine profil
0
iul. 25
1153
Why use the Lazy Translation function _lt()
javascript
Imagine profil
Imagine profil
1
iul. 25
6460
Comunitate
  • Tutorials
  • Documentație
  • Forum
Open Source
  • Descărcare
  • Github
  • Runbot
  • Translations
Servicii
  • Hosting Odoo.sh
  • Suport
  • Actualizare
  • Custom Developments
  • Educație
  • Găsiți un contabil
  • Găsește un Partener
  • Devino Partener
Despre Noi
  • Compania noastră
  • Active de marcă
  • Contactați-ne
  • Locuri de muncă
  • Evenimente
  • Podcast
  • Blog
  • Clienți
  • Aspecte juridice • Confidențialitate
  • Securitate
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo este o suită de aplicații de afaceri open source care acoperă toate nevoile companiei dvs.: CRM, comerț electronic, contabilitate, inventar, punct de vânzare, management de proiect etc.

Propunerea de valoare unică a Odoo este să fie în același timp foarte ușor de utilizat și complet integrat.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now