简体   繁体   English

在 BigCommerce Stencil 中连接自定义按钮

[英]Wiring up a custom button in BigCommerce Stencil

I'm trying to wrap my head around how to wire up a simple button in BigCommerce Stencil.我正在努力思考如何在 BigCommerce Stencil 中连接一个简单的按钮。 I've been using this platform for about 24 hours now, so any help you can give is MUCH appreciated.. I haven't used Handlebars.js or jQuery in a few years so I'm pretty rusty.我已经使用这个平台大约 24 小时了,因此非常感谢您提供的任何帮助。我已经有几年没有使用 Handlebars.js 或 jQuery 了,所以我已经很生疏了。

I'm using the Cornerstone Theme.我正在使用基石主题。

What I'm looking to do is:我要做的是:

  1. Click a button单击一个按钮
  2. Array of objects sent to my JS function发送到我的 JS function 的对象数组
  3. JS Function adds all the items in the array to the cart. JS Function 将数组中的所有项目添加到购物车。

I feel like this shouldn't be that hard, but where I am getting stuck is.我觉得这不应该那么难,但我卡住的地方是。

  1. Getting data that is available in the HTML to be available to my function.获取 HTML 中可用的数据以供我的 function 使用。
    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');
    }

And on the HTML side, I have在 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'}}" />

The technically correct way of doing this would be to utilize the inject helper.技术上正确的方法是使用注入助手。 This passes data through to the JS Context within the theme JavaScript files.这会将数据传递到主题 JavaScript 文件中的 JS 上下文。 Assuming you are in a file with access to this context (such as category.js), you could use the following code.假设您在一个可以访问此上下文的文件中(例如 category.js),您可以使用以下代码。

In your HTML: {{inject "categoryProducts" category.products}}在您的 HTML 中: {{inject "categoryProducts" category.products}}

In your JS: console.log(this.context.categoryProducts);在您的 JS 中: console.log(this.context.categoryProducts);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM