Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
1 Antworten
1704 Ansichten

Hello everyone!
i need help! i am trying to inherit a constructor from the file lazy_barcode_cache.js and Is not working, I tried with patch but nothing happends. 

export default class LazyBarcodeCache {

    constructor(cacheData, params) {

        this.rpc = params.rpc;

        this.dbIdCache = {}; // Cache by model + id

        this.dbBarcodeCache = {}; // Cache by model + barcode

        this.missingBarcode = new Set(); // Used as a cache by `_getMissingRecord`

        this.barcodeFieldByModel = {

            'stock.location': 'barcode',

            'product.product': 'barcode',

            'product.packaging': 'barcode',

            'stock.package.type': 'barcode',

            'stock.picking': 'name',

            'stock.quant.package': 'name',

            'stock.lot': 'name', // Also ref, should take in account multiple fields ?

        };

        this.gs1LengthsByModel = {

            'product.product': 14,

            'product.packaging': 14,

            'stock.location': 13,

            'stock.quant.package': 18,

        };

        // If there is only one active barcode nomenclature, set the cache to be compliant with it.

        if (cacheData['barcode.nomenclature'].length === 1) {

            this.nomenclature = cacheData['barcode.nomenclature'][0];

        }

        this.setCache(cacheData);

    }


thats the one i need to inherit, and the only change i need to do is change this:
"

            'stock.lot': 'name', 


to :

            'stock.lot': 'barcode_lot',

if somebody could help i will be super grateful. 

Greetings! 

Avatar
Verwerfen
Beste Antwort

Hi,

Try this

 /** @odoo-module **/


import LazyBarcodeCache from '@stock_barcode/lazy_barcode_cache';


import { patch } from "@web/core/utils/patch";


patch(LazyBarcodeCache.prototype, {

    /**

     * @override

     */

     _constructor() {

        super._constructor(...arguments);

    },

});


Add the file in the correct asset bundle.

'assets': {

        'web.assets_backend': [

             "Add your file"

        ],

    },


Hope it helps

Avatar
Verwerfen
Autor

Hello!
I tried that before and didnt work... Later on I read this in the odoo's documentation

"Also, Javascript handles the constructor in a special native way which makes it impossible to be patched. The only workaround is to call a method in the original constructor and patch that method instead:"

So after trying and trying haha later I found this that worked:
/** @odoo-module **/
import { patch } from "@web/core/utils/patch";
import LazyBarcodeCache from "@stock_barcode/lazy_barcode_cache";

const originalSetCache = LazyBarcodeCache.prototype.setCache;

// Aplica el parche al prototipo de la clase LazyBarcodeCache
patch(LazyBarcodeCache.prototype, {
setCache(cacheData) {
// Llama al método original usando la referencia guardada
originalSetCache.call(this, cacheData);

// Modifica barcodeFieldByModel después de la inicialización
if (this.barcodeFieldByModel) {
this.barcodeFieldByModel['stock.lot'] = 'barcode_lot';
} else {
console.error("barcodeFieldByModel is undefined.");
}
},
});

I actually call the setCache first with the original argument (cacheData) that contains "barcodeFieldByModel" to changed it after de initialization and it worked pretty well.

Thank you so much for the answer,
greetings!

Verknüpfte Beiträge Antworten Ansichten Aktivität
2
Juli 25
699
0
März 25
1150
1
Sept. 24
1309
1
Juli 24
2257
1
März 24
1694