简体   繁体   English

如何在YUI模块中运行代码?

[英]How do I run the code in a YUI module?

I have a page which has product images (representing product colours) and a product colour selection box. 我有一个页面,其中包含产品图像(代表产品颜色)和产品颜色选择框。 This page was built using Squarespace Commerce and uses YUI. 该页面是使用Squarespace Commerce构建的,并使用YUI。

If you change the product colour using the selection box, some code is run which updates a javascript variable to reflect the product you've chosen. 如果使用选择框更改产品颜色,则会运行一些代码,这些代码将更新javascript变量以反映您选择的产品。

I am trying to alter the page so that when a user selects an image, the select box is automatically updated. 我正在尝试更改页面,以便当用户选择图像时,选择框会自动更新。 I have successfully achieved this (the select box changes to the relevant colour), but there is a YUI module which contains code which is not being run. 我已经成功实现了这一点(选择框更改为相关颜色),但是有一个包含未运行代码的YUI模块。 It is this code which updates the javascript variable to reflect the chosen product. 正是此代码更新了javascript变量以反映所选的产品。

I've tried to run this code by triggering the change event on the select box but it has no impact. 我试图通过触发选择框上的change事件来运行此代码,但这没有影响。

This is the YUI code: 这是YUI代码:

YUI.add("squarespace-product-utils", function (a) {
    a.Squarespace.ProductUtils = {
        initializeVariantDropdowns: function () {
            a.all(".product-variants[data-variants]").each(function (e) {
                var b = JSON.parse(e.getAttribute("data-variants")),
                    c = e.all("select"),
                    d = e.siblings(".product-price").item(0),
                    g;
                d && (g = d.getHTML());
                a.Squarespace.ProductUtils._checkVariantStockAndPrice(e, b, c, d, g);
                c.detach("change");
                c.each(function (f) {
                        f.after("change", function (f) {
                            a.Squarespace.ProductUtils._checkVariantStockAndPrice(e, b, c, d, g)
                        }, this)
                    },
                    this)
            }, this)
        },
        _checkVariantStockAndPrice: function (e, b, c, d, g) {
            e.removeAttribute("data-unselected-options");
            e.removeAttribute("data-selected-variant");
            e.removeAttribute("data-variant-in-stock");
            var f = e.one(".variant-out-of-stock");
            f && f.remove();
            var h = [],
                f = null,
                k = !1,
                l = {};
            c.each(function (a) {
                var b = a.get("value");
                a = a.getAttribute("data-variant-option-name");
                "none" != b ? l[a] = b : h.push(a)
            }, this);
            if (0 === h.length) {
                for (c = 0; c < b.length; c++) {
                    g = b[c];
                    var t = !0,
                        n;
                    for (n in l)
                        if (l[n] != g.attributes[n]) {
                            t = !1;
                            break
                        }
                    if (t) {
                        f =
                            g;
                        if (g.unlimited || 0 < g.qtyInStock) k = !0;
                        break
                    }
                }!f && d ? d.set("text", "Unavailable") : d && (f.onSale ? (d.setHTML(a.Squarespace.Commerce.moneyString(f.salePrice)), d.append(a.Node.create('<span> </span><span class="original-price">' + a.Squarespace.Commerce.moneyString(f.price) + "</span>"))) : d.setHTML(a.Squarespace.Commerce.moneyString(f.price)));
                f && !k && e.append(a.Node.create('<div class="variant-out-of-stock">Out of stock.</div>'))
            } else d && d.getHTML() !== g && d.setHTML(g);
            e.setAttribute("data-unselected-options",
                JSON.stringify(h));
            f && e.setAttribute("data-selected-variant", JSON.stringify(f));
            k && e.setAttribute("data-variant-in-stock", k)
        }
    }
}, "1.0", {
    requires: ["base", "node", "squarespace-commerce-utils"]
});

This is the javascript code I've added to update the select box and (try) to trigger the change event: 这是我添加的用于更新选择框和(尝试)触发更改事件的javascript代码:

  $(document).on('click', '#productThumbnails .slide', function() {
    var colorindex = $(this).index() + 2;    
    $('#productDetails .product-variants select').val($('#productDetails .product-variants select :nth-child(' + colorindex + ')').val()).change();
  });

My code successfully updates the select box but it does not seem to trigger the change event. 我的代码成功更新了选择框,但似乎没有触发更改事件。 I think the clue is around the f.after("change", ... but I know nothing about YUI and cannot find anything to help. 我认为这是f.after("change", ...周围的线索f.after("change", ...但是我对YUI一无所知,找不到任何帮助。

UPDATE: It does trigger a change event if I add a change event using jQuery, but it still won't trigger the change event which has been added by YUI. 更新:不会触发更改事件如果我添加使用jQuery change事件,但它仍然不会触发已由YUI添加更改事件。

How do I trigger this code? 如何触发此代码?

You can see the (failing) code in operation here . 您可以在此处查看正在运行的(失败)代码。

To trigger a YUI module you use the use function, which references the original instance like this: 要触发一个YUI模块,可以使用use函数,该函数引用原始实例,如下所示:

YUI().use('squarespace-product-utils', function (a) {
  a.Squarespace.ProductUtils.initializeVariantDropdowns();
});   

Online reference available here . 可在此处在线参考

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

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