Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
1406 Widoki

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! 

Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć
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!

Powiązane posty Odpowiedzi Widoki Czynność
2
lip 25
407
0
mar 25
965
1
wrz 24
1111
1
lip 24
1980
1
mar 24
1552