如何在 pos odoo13 中覆盖 models.js 中的模型

How to override the models in models.js in pos odoo13

如何将域添加到以下模型并在销售点启动时加载它。

{
    model:  'res.partner',
    label: 'load_partners',
    fields: ['name','street','city','state_id','country_id','vat',
             'phone','zip','mobile','email','barcode','write_date',
             'property_account_position_id','property_product_pricelist'],
    loaded: function(self,partners){
        self.partners = partners;
        self.db.add_partners(partners);
    },
}

使用point_of_sale.modelsload_models函数加载res.partner模型,(你可以在原始模型文件中找到许多使用domain的模型):

odoo.define('my_module.partners', function (require) {
"use strict";

    var models = require('point_of_sale.models');

    models.load_models([{
        model:  'res.partner',
        fields: ['name','street','city','state_id','country_id','vat', 'phone','zip','mobile','email','barcode','write_date', 'property_account_position_id','property_product_pricelist'],
        domain: function(self){ return [['company_id', '=', self.config.company_id[0]]]; },
        loaded: function(self, partner) {}
    }]);
});

domain: [domain|function]决定需要加载哪些模型的域。 Null 加载所有内容

编辑:

您可以使用 models.PosModel.prototype.models 访问 models,您还需要覆盖 prepare_new_partners_domain 方法。

试试下面的例子:

var _super_pos_model = models.PosModel.prototype;
var _models = models.PosModel.prototype.models;

var _domain = [['id', '<=', 3]];
// partner model is the fifth element in models (index==4)
_models[4]['domain']  = function(self){ return _domain; };

models.PosModel = models.PosModel.extend({
     prepare_new_partners_domain: function(){
        var domain = _super_pos_model.prepare_new_partners_domain.apply(this, arguments);
        domain.push(..._domain);
        console.log("domain", domain);
        return domain;
    },
});

console.log("models", models.PosModel.prototype.models);

您可以使用它来覆盖 POS 上的加载模型

var pos_models = require('point_of_sale.models');

var _pos_super = pos_models.PosModel.prototype;
pos_models.PosModel = pos_models.PosModel.extend({
    initialize: function(session, attributes) {
        for (var i = 0 ; i < this.models.length; i++){
            if (this.models[i].model == 'res.partner') {
                this.models[i].fields = ['your_fields'];
                this.models[i].domain = [['sample_column', '=', true]];
            }
        }
        return _pos_super.initialize.apply(this, arguments);
    }
});