值在JS Odoo POS中是未定义的返回模型



我正在尝试覆盖POS Odoo 中的订单行价格

我的价格.js

get_unit_display_price: function(){
var self = this;                
var line = self.export_as_JSON();
var product = this.pos.db.get_product_by_id(line.product_id);        
fields.product_id  = line.product_id;
fields.pricelist_id   = this.pos.config.pricelist_id[0];
fields.uom = product.uom_id;
fields.line_qty = line.qty;
fields.price_unit = line.price_unit;
var model = new Model('pos.order');
this.total_price = model.call('calculate_price',
[0, fields]).done(function(result){
total_price = result['total_price'];
return  result['total_price'];
});
}

price.xml

<t t-jquery=".price" t-operation="append">      
<t t-esc="widget.format_currency(line.get_unit_display_price)"/>
</t>

我正在从Model(price.py)中获取值total_price但在xml文件的get_unit_display_price中返回的是未定义的。

在执行新的模型函数后,如何从js中设置xml中的值(js值来自模型)?。

您的代码中有很多问题,我可以列出一些:

  1. 在扩展Orderline模型的price.js中,您从后端调用了函数"caculate_price"=>它是异步函数,所以我不能立即为您返回值=>您的函数在调用成功之前返回undefined
  2. 您不需要export_as_JSON(),您可以直接从Orderline对象中获得您想要的值(字段:product_id,uom,qty,price_unit)
  3. 在你的"price.xml"中,你想从模型中调用一个函数,你错过了父主题,它应该是这样的line.get_unit_display_price()

如何在执行新模型函数后从js在xml中设置值(js值来自模型)?。

有两个选项:

  • 选项1:通过rpc调用服务器上py文件的方法,然后像您那样等待响应结果(我不建议这样做)。因此,当调用完成后,您应该获得一个DOM,其中的值应该以HTML显示,然后将值更新到它
  • 选项2:我建议您实现一种方法"calculate_price",它将执行与Orderline模型中的服务器相同的逻辑,这样您的POS就可以在没有互联网的情况下工作(半离线模式)。然后您可以很容易地从xml文件中调用它。这意味着您在price.js中编写函数calculate_price,然后在get_unit_display_price中调用它

希望它能有所帮助,我希望你能做选项2。

get_orderline: function() {
var order = this.pos.get_order();
var orderlines = order.orderlines.models;
var all_lines = [];
for (var i = 0; i < orderlines.length; i++) {
var line = orderlines[i]
if (line) {
all_lines.push({
'product_id': line.product.id,
'qty': line.quantity,
'price': line.get_display_price(),
})
}
}
return all_lines
},

最新更新