javascript - 在 BigCommerce Stencil 中连接自定义按钮

标签 javascript handlebars.js bigcommerce

我正在努力思考如何在 BigCommerce Stencil 中连接一个简单的按钮。我已经使用这个平台大约 24 小时了,所以非常感谢您提供的任何帮助!几年来我没有使用过 Handlebars.js 或 jQuery,所以我很生疏。
我正在使用基石主题。
我要做的是:

  • 点击按钮
  • 发送到我的 JS 函数的对象数组
  • JS 函数将数组中的所有项目添加到购物车。

  • 我觉得这不应该那么难,但我卡住的地方是。
  • 获取 HTML 中可用的数据以供我的函数使用。

  •     handleButtons() {
            $('#add-all-to-cart').on('click', (event) => this.addAllItemsToCart(event));
            $('#remove-all-from-cart').on('click', (event) => this.removeAllFromCart(event));
        }
    
        //I want to supply {{category.products}} from the HTML
        addAllItemsToCart(e) {
            console.log('Adding all items to cart');
        }
    
        removeAllFromCart(e) {
            console.log('Removing all items from cart');
        }
    
    在 HTML 方面,我有
            //This seems to be the way other buttons were made in the Theme
            <input  id='add-all-to-cart' 
                 data-wait-message="{{lang 'products.adding_to_cart'}}"
                 class="button button--small"
                 type="submit"
                 value="{{lang 'products.add_all_to_cart'}}" />
            <input
                id="remove-all-from-cart"
                data-wait-message="{{lang 'products.adding_to_cart'}}"
                class="button button--small"
                type="submit"
                value="{{lang 'products.remove_all_from_cart'}}" />
    

    最佳答案

    技术上正确的方法是使用 inject helper 。这会将数据传递到主题 JavaScript 文件中的 JS 上下文。假设您在一个可以访问此上下文的文件中(例如 category.js),您可以使用以下代码。
    在您的 HTML 中:{{inject "categoryProducts" category.products}}在您的 JS 中:console.log(this.context.categoryProducts);

    关于javascript - 在 BigCommerce Stencil 中连接自定义按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66299366/

    相关文章:

    javascript - ChaiJS 应该 - 测试空字符串

    javascript - 如何将 XML 值读入 Javascript 或 jQuery

    node.js - 将node js查询结果分离成对象?

    php - 无法连接到bigcommerce api

    c# - Bigcommerce:卸载回调 URL

    bigcommerce - 在基石 2.0 中找不到模块 webpack.dev.js

    javascript - 过滤数组内的数组

    javascript - 实时区域不适用于动态页面标题 - angularjs

    javascript - 如何在 Node 中注册和使用 Handlebars 助手?

    javascript - 如何访问具有 json 字符串的 javascript 对象中的特定键值对