为什么在ODOO12中无法正常运行在ODOO14中正常运行的JS监听器?

huangapple go评论52阅读模式
英文:

Why can't the JS listener that runs normally in ODOO14 run properly in ODOO12?

问题

odoo.define('product_dimensions.website_sale', function (require) {
"use strict";

var core = require('web.core');
var _t = core._t;
var ajax = require('web.ajax');
var publicWidget = require('web.public.widget');

publicWidget.registry.ProductDimensions = publicWidget.Widget.extend({
    selector: '.oe_website_sale',
    events: {
        'change input[name="wide"]': '_onWidthChange',
        'change input[name="high"]': '_onHeightChange',
    },

    start: function () {
        this.product_id = this.$el.find('#product_id').val(); // 假设'#product_id'是一个包含产品ID的输入字段
        return this._super.apply(this, arguments);
    },

    _onWidthChange: function () {
        var wide = parseFloat($('input[name="wide"]').val());
        console.log(this.product_id)
        ajax.jsonRpc("/shop/product/min_dimensions", 'call', {'product_id': this.product_id}).then(function (data) {
            if (wide < data.wide_mini) {
                alert(_t('您输入的数值小于最小宽度:') + data.wide_mini);
            }
            else if (wide > data.wide_max) {
                alert(_t('您输入的数值大于最大宽度:') + data.wide_max);
            }
        });
    },

    _onHeightChange: function () {
        var high = parseFloat($('input[name="high"]').val());
        ajax.jsonRpc("/shop/product/min_dimensions", 'call', {'product_id': this.product_id}).then(function (data) {
            if (high < data.high_mini) {
                alert(_t('您输入的数值小于最小高度:') + data.high_mini);
            }
            else if (high > data.high_max) {
                alert(_t('您输入的数值大于最大高度:') + data.high_max);
            }
        });
    },
});

return publicWidget.registry.ProductDimensions;

});

为什么在ODOO12中无法正确运行在ODOO14中正常运行的JS监听器?上述代码可以在ODOO14网站商店的产品详情页面中输入宽度和高度。此时,输入的数值将与后端产品页面中设置的宽度和高度进行比较。如果小于宽度和高度或大于宽度和高度的值,将弹出错误提示,要求输入正确的数值。

英文:
odoo.define(&#39;product_dimensions.website_sale&#39;, function (require) {
    &quot;use strict&quot;;

    var core = require(&#39;web.core&#39;);
    var _t = core._t;
    var ajax = require(&#39;web.ajax&#39;);
    var publicWidget = require(&#39;web.public.widget&#39;);

    publicWidget.registry.ProductDimensions = publicWidget.Widget.extend({
        selector: &#39;.oe_website_sale&#39;,
        events: {
            &#39;change input[name=&quot;wide&quot;]&#39;: &#39;_onWidthChange&#39;,
            &#39;change input[name=&quot;high&quot;]&#39;: &#39;_onHeightChange&#39;,
        },

        start: function () {
            this.product_id = this.$el.find(&#39;#product_id&#39;).val(); // Assuming &#39;#product_id&#39; is an input field that holds the product id
            return this._super.apply(this, arguments);
        },

        _onWidthChange: function () {
            var wide = parseFloat($(&#39;input[name=&quot;wide&quot;]&#39;).val());
            console.log(this.product_id)
            ajax.jsonRpc(&quot;/shop/product/min_dimensions&quot;, &#39;call&#39;, {&#39;product_id&#39;: this.product_id}).then(function (data) {
                if (wide &lt; data.wide_mini) {
                    alert(_t(&#39;The number you entered is less than the minimum width:&#39;)+ data.wide_mini);
                }
                else if (wide &gt; data.wide_max) {
                    alert(_t(&#39;The number you entered is greater than the maximum width:&#39;)+ data.wide_max);
                }
            });
        },

        _onHeightChange: function () {
            var high = parseFloat($(&#39;input[name=&quot;high&quot;]&#39;).val());
            ajax.jsonRpc(&quot;/shop/product/min_dimensions&quot;, &#39;call&#39;, {&#39;product_id&#39;: this.product_id}).then(function (data) {
                if (high &lt; data.high_mini) {
                    alert(_t(&#39;The number you entered is less than the minimum height:&#39;)+ data.high_mini);
                }
                else if (high &gt; data.high_max) {
                    alert(_t(&#39;The number you entered is greater than the maximum height:&#39;)+ data.high_max);
                }
            });
        },
    });

    return publicWidget.registry.ProductDimensions;
});

Why can't the JS listener that runs normally in ODOO14 run correctly in ODOO12? The above code can input width and height in the product details page in the ODOO14 website store. At this time, the input number will be compared with the width and height set in the backend product page. If it is less than the width and height or greater than the value set by the width and height, an error prompt will pop up and require inputting the correct number.

答案1

得分: 1

因为在Odoo 12.0中没有名为web.public.widget的小部件。

这是一个自Odoo 13.0版本开始存在的小部件。

英文:

Because in Odoo 12.0 there is no widget with the name: web.public.widget

That's a widget that exists since Odoo version 13.0

答案2

得分: 0

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->

var sAnimations = require('website.content.snippets.animation');

    sAnimations.registry.ProductDimensions = sAnimations.Class.extend({
        selector: '.oe_website_sale',
        events: {
            'change input[name="wide"]': '_onWidthChange',
            'change input[name="high"]': '_onHeightChange',
        },

<!-- 结束代码片段 -->

问题已解决

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var sAnimations = require(&#39;website.content.snippets.animation&#39;);

    sAnimations.registry.ProductDimensions = sAnimations.Class.extend({
        selector: &#39;.oe_website_sale&#39;,
        events: {
            &#39;change input[name=&quot;wide&quot;]&#39;: &#39;_onWidthChange&#39;,
            &#39;change input[name=&quot;high&quot;]&#39;: &#39;_onHeightChange&#39;,
        },

<!-- end snippet -->

Problem solved

huangapple
  • 本文由 发表于 2023年8月9日 05:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定