تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
1 الرد
1460 أدوات العرض

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!

المنشورات ذات الصلة الردود أدوات العرض النشاط
2
يوليو 25
413
0
مارس 25
972
1
سبتمبر 24
1136
1
يوليو 24
2041
1
مارس 24
1561