Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
1 Відповісти
1464 Переглядів

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! 

Аватар
Відмінити
Найкраща відповідь

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

Аватар
Відмінити
Автор

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!

Related Posts Відповіді Переглядів Дія
2
лип. 25
414
0
бер. 25
972
1
вер. 24
1137
1
лип. 24
2042
1
бер. 24
1561