knockout.js Knockout Js magento 2在结帐页面上

dzhpxtsq  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(193)

我已经创建了模板.html

  1. <div data-bind="foreach: productOverGift, visible: expanded" class="free-gift-oder75">
  2. <div class="product-over-info">
  3. <input type="radio" class="product-information" name="productId" data-bind="attr: { value: id, id:'product_over_'+id}, click: $parent.selectItem(id, sku)"/>
  4. <image data-bind="attr: { src: image}" class="product-image" width="120" height="150" alt="Product Image" style="width: 50px; height: 50px"/>
  5. <span data-bind="text: name"></span>
  6. </div>
  7. </div>

并创建文件js

  1. define(
  2. [
  3. 'jquery',
  4. 'ko',
  5. 'uiComponent',
  6. 'underscore',
  7. 'Magento_Checkout/js/model/step-navigator',
  8. 'Magento_Customer/js/model/customer',
  9. 'Magento_Checkout/js/model/quote',
  10. 'mage/url',
  11. 'mage/storage',
  12. 'Magento_Checkout/js/action/get-totals',
  13. 'mage/translate',
  14. 'Magento_Ui/js/model/messageList',
  15. 'Magento_Checkout/js/model/full-screen-loader'
  16. ],
  17. function (
  18. $,
  19. ko,
  20. Component,
  21. _,
  22. stepNavigator,
  23. customer,
  24. quote,
  25. urlBuilder,
  26. storage,
  27. getTotal,
  28. $t,
  29. messageList,
  30. startLoader
  31. ) {
  32. 'use strict';
  33. /**
  34. * check-login - is the name of the component's .html template
  35. */
  36. return Component.extend({
  37. defaults: {
  38. template: 'Custom_Checkout/promotion'
  39. },
  40. //add here your logic to display step,
  41. isVisible: ko.observable(true),
  42. isLogedIn: customer.isLoggedIn(),
  43. //step code will be used as step content id in the component template
  44. stepCode: 'promotion-step',
  45. //step title value
  46. stepTitle: 'Logging Status',
  47. productOverGift: ko.observableArray([]),
  48. freeGiftWithPurchase: ko.observableArray([]),
  49. productImageSelected: ko.observable(""),
  50. productNameSelected: ko.observable(""),
  51. idProductSelected: ko.observable(""),
  52. quoteId: ko.observable(""),
  53. isChecked: ko.observable(true),
  54. skuProductSelected: ko.observable(""),
  55. messageError: ko.observable(""),
  56. message: ko.observable(""),
  57. expanded: ko.observable(true),
  58. arrowClass: ko.observable('fa fa-angle-double-up'),
  59. arrowTitle: ko.observable($t('Shrink')),
  60. dataShippingPnr: ko.observable(""),
  61. dataShippingFullName: ko.observable(""),
  62. dataShippingEmail: ko.observable(""),
  63. dataSegmentDisplay: ko.observable(""),
  64. isCheckedOver: ko.observable(true),
  65. isDeleted: false,
  66. /**
  67. *
  68. * @returns {*}
  69. */
  70. initialize: function () {
  71. this._super();
  72. //register your step
  73. stepNavigator.registerStep(
  74. this.stepCode,
  75. //step alias
  76. null,
  77. this.stepTitle,
  78. //observable property with logic when display step or hide step
  79. this.isVisible,
  80. _.bind(this.navigate, this),
  81. /**
  82. * sort order value
  83. * 'sort order value' < 10: step displays before shipping step;
  84. * 10 < 'sort order value' < 20 : step displays between shipping and payment step
  85. * 'sort order value' > 20 : step displays after payment step
  86. */
  87. 15
  88. );
  89. return this;
  90. },
  91. /**
  92. * select radio
  93. * @param id
  94. * @param quoteId
  95. * @param sku
  96. */
  97. selectItem: function (id, sku) {
  98. var self = this;
  99. var listProduct = this.productOverGift();
  100. var itemChecked = $('#product_over_' + id).is(":checked");
  101. if (itemChecked) {
  102. if (listProduct.length > 0) {
  103. listProduct.forEach(function (item, i) {
  104. if (item.id === id) {
  105. self.productImageSelected(item.image);
  106. self.productNameSelected(item.name);
  107. self.idProductSelected(item.id);
  108. self.skuProductSelected(item.sku);
  109. }
  110. });
  111. }
  112. // self.deleteItemInCart(oldProductSku, this.quoteId());
  113. // self.saveProductToOrder(id, this.quoteId(), sku);
  114. }
  115. },
  116. });
  117. }

);
我有一个函数“selectItem()”在input =〉标签的循环中使用,在单击(选中)后将调用“selectItem“函数。但现在“selectItem“函数总是在页面加载时调用,这不是我想要的。我只是希望它在单击input后被调用。请帮助我。谢谢

rsaldnfx

rsaldnfx1#

此时,您正在HTML中执行函数,而不是将对它的引用绑定到click处理程序。这将在加载页面时执行该函数:

  1. click: $parent.selectItem(id, sku)

相反,您应该将对函数的引用绑定到单击处理函数:

  1. click: $parent.selectItem

访问idsku属性的方法有很多种,但最简单的方法可能是意识到传递给click处理程序的第一个参数是forEach: productOverGift中的当前模型。因此,将selectItem函数的签名重写为如下所示:

  1. selectItem: function (data) {
  2. let id = data.id
  3. let sku = data.sku
  4. ...

这应该可以让它为你工作。然而,你可能会考虑看看checked绑定。这可以帮助你整理你的代码,以一种更像KO的风格来完成它,但这不是必需的。如果你有兴趣解决这个问题,那么你可以直接把radio的值绑定到itemChecked属性,并使用一个观察器来运行你的selectItem代码:

  1. produceViewModel.itemChecked.subscribe(function(newValue) {
  2. if (newValue) {
  3. // select product image here, etc
  4. }
  5. });

或者,productImage选择属性可以是从itemChecked导出的计算值。
这将消除代码中的jQuery依赖性,并开始以一种更被动的KO风格来完成这一切,但要做到这一点,您需要将一些代码从$parent中移到产品VM中。

展开查看全部

相关问题