【问题标题】:jQuery "change" event removed when in Knockout scope在 Knockout 范围内删除 jQuery“更改”事件
【发布时间】:2017-07-14 23:54:25
【问题描述】:

我不知道它是否特定于 Magento 2,但是一个已经设置的带有 jQ​​uery change 事件的 HTML 节点在带有附加 VM 的淘汰组件中运行时将丢失其 change 事件(查看模型)。

Magento 中的情况:

我通过布局 xml 将 product options wrapper 移动到自定义块中

<referenceBlock name="product.info.form.options">
    // Custom block definition
    <move element="product.info.options.wrapper" destination="custom.block" />
</referenceBlock>

没有任何问题!

现在,当用 Magentos custom "scope" binding 围绕这个移动块包装一个淘汰模型时,所有应用的 onChange 事件都将被删除 - 这意味着在这种情况下,可配置选项选择框保持禁用状态。

change 事件正在被应用 here in configurable.js

$(element).on('change', this, this._configure);

.. 这对我来说似乎也适合如何添加事件。

因此,在我的模板文件中,我执行以下操作来围绕这个 html sn-p 包装我的淘汰组件(带有附加的 VM):

<div class="tab-content" data-step="1" data-bind="scope:'times'">
    // all change events are removed from the configurable widget 
    <?php echo $block->getChildHtml('product_options_wrapper'); ?>
</div>

有人知道这是 KnockoutJS 和 jQuery 之间的问题,还是与 Magento 有关?

更新:

这似乎是 Magento 中的一个错误: https://github.com/magento/magento2/issues/10261

【问题讨论】:

    标签: jquery magento knockout.js magento2


    【解决方案1】:

    您需要在应用 ko 绑定后应用事件侦听器,并在可观察对象发生更改后重新应用它们。这可能是由于运行事件侦听器时 html 不存在。

    请问你为什么使用jquery事件监听器来改变值,如果输入被ko绑定,你可以使用订阅:

    self.Value.subscribe(function (newValue) {
        // Logic in here
    });
    

    【讨论】:

    • 原始功能是默认的 Magento - 产品详细信息页面,选择框中的多个可配置属性......并且 Magento 使用 jQuery 事件侦听器 ;-) 这很好,但我需要这个完整的块KO 模板将自定义值应用于标记围绕此块过滤器块。那有意义吗? oO
    • 此外:我将如何再次应用来自 Magento 核心 JS(见上面的链接)的事件触发器?
    • 这个$(element).on('change', this, this._configure);需要在你的ko.applyBindings(viewModel, document.getElementById("container"));之后被解雇
    • 要获得所有选择的elements,我将从configurable.js 小部件中复制代码。我不确定是否使用这种方法,但会尝试检查。
    猜你喜欢
    • 2012-08-26
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 2014-06-08
    相关资源
    最近更新 更多