更新ajax加载的knockoutJS元素的绑定

时间:2018-10-10 10:20:18

标签: knockout.js

这可能不是淘汰赛方法,但已通过ajax加载了边栏并希望对此进行更新。但是,已存在的剔除元素无法正确填充,并且似乎填充有来自剔除元素初始版本的数据。侧边栏的原始html如下所示:

 <div class=\"block filter\" id=\"layered-filter-block\" data-mage-init='{\"collapsible\":{\"openedState\": \"active\", \"collapsible\": true, \"active\": false, \"collateral\": { \"openedState\": \"filter-active\", \"element\": \"body\" } }}'>
<div class=\"block-title filter-title\" data-count=\"0\">
<strong data-role=\"title\">Shop By<\/strong>
<\/div>
<div class=\"block-content filter-content\">
    <strong role=\"heading\" aria-level=\"2\" class=\"block-subtitle filter-subtitle\">Shopping Options<\/strong>
        <div class=\"filter-options\" id=\"narrow-by-list\" data-role=\"content\" data-mage-init='{\"accordion\":{\"openedState\": \"active\", \"collapsible\": true, \"active\": [0,1,2], \"multipleCollapsible\": true}}'>
            <div data-role=\"collapsible\" class=\"filter-options-item\">
                <div data-role=\"title\" class=\"filter-options-title\">Category<\/div>
                <div data-role=\"content\" class=\"filter-options-content\">\n<ol class=\"items\">
                    <li class=\"item\">
                        <a href=\"http:\/\/www.domain.com\/catalogsearch\/result\/index\/?ajax=1&amp;cat=143&amp;q=ice+machine\">Front of House
                        <span class=\"count\">2<span class=\"filter-count-label\">items<\/span><\/span><\/a>
                    <\/li>
                    <li class=\"item\">
                        <a href=\"http:\/\/www.domain.com\/catalogsearch\/result\/index\/?ajax=1&amp;cat=182&amp;q=ice+machine\">Bar Supplies
                        <span class=\"count\">4<span class=\"filter-count-label\">items<\/span><\/span><\/a>
                    <\/li>
                    <li class=\"item\">
                        <a href=\"http:\/\/www.domain.com\/catalogsearch\/result\/index\/?ajax=1&amp;cat=257&amp;q=ice+machine\">Catering Equipment<span class=\"count\">111<span class=\"filter-count-label\">\n 
                        items                        <\/span><\/span>\n       
                        <\/a>\n                    <\/li>\n 
                        <li class=\"item\">\n                    
                        <a href=\"http:\/\/www.domain.com\/catalogsearch\/result\/index\/?ajax=1&amp;cat=342&amp;q=ice+machine\">\n           
                        Warewashing                                            <span class=\"count\">\n                        
                        3                        <span class=\"filter-count-label\">\n           
                        items                        <\/span><\/span>\n                     
                        <\/a>\n                    <\/li>\n            <li class=\"item\">\n         
                        <a href=\"http:\/\/www.domain.com\/catalogsearch\/result\/index\/?ajax=1&amp;cat=521&amp;q=ice+machine\">\n          
                        Catering Equipment Offers                                            <span class=\"count\">\n                            1         
                        <span class=\"filter-count-label\">\n                                                    item                        <\/span><\/span>\n         
                        <\/a>\n                    <\/li>\
                        <\/ol>
                        <\/div>\n                    
                        <\/div>\n                                                        
                        <div data-role=\"collapsible\" class=\"filter-options-item\">                
                        <div data-role=\"title\" class=\"filter-options-title\">Brand<\/div>\n    

                        <div data-role=\"content\" class=\"filter-options-content\">
                            <div data-bind=\"scope: 'brandFilter'\">
                                <!-- ko template: getTemplate() --> <!-- \/ko -->
                            <\/div>
                            <script type=\"text\/x-magento-init\">
                                {\"*\" : {\"Magento_Ui\/js\/core\/app\": {\"components\": {\"brandFilter\": {\"component\":\"Smile_ElasticsuiteCatalog\\\/js\\\/attribute-filter\",\"maxSize\":10,\"displayProductCount\":true,\"hasMoreItems\":true,\"ajaxLoadUrl\":\"http:\\\/\\\/www.domain.com\\\/catalog\\\/navigation_filter\\\/ajax\\\/?ajax=1&filterName=brand&q=ice+machine\",\"items\":[{\"label\":\"Scotsman\",\"count\":41,\"url\":\"http:\\\/\\\/www.domain.com\\\/catalogsearch\\\/result\\\/index\\\/?ajax=1&brand=Scotsman&q=ice+machine\",\"is_selected\":false},{\"label\":\"Hoshizaki\",\"count\":15,\"url\":\"http:\\\/\\\/www.domain.com\\\/catalogsearch\\\/result\\\/index\\\/?ajax=1&brand=Hoshizaki&q=ice+machine\",\"is_selected\":false},{\"label\":\"Ice-o-matic\",\"count\":12,\"url\":\"http:\\\/\\\/www.domain.com\\\/catalogsearch\\\/result\\\/index\\\/?ajax=1&brand=Ice-o-matic&q=ice+machine\",\"is_selected\":false},{\"label\":\"Blue Ice\",\"count\":7,\"url\":\"http:\\\/\\\/www.domain.com\\\/catalogsearch\\\/result\\\/index\\\/?ajax=1&brand=Blue+Ice&q=ice+machine\",\"is_selected\":false},{\"label\":\"Graupel\",\"count\":7,\"url\":\"http:\\\/\\\/www.domain.com\\\/catalogsearch\\\/result\\\/index\\\/?ajax=1&brand=Graupel&q=ice+machine\",\"is_selected\":false},{\"label\":\"Nemox\",\"count\":7,\"url\":\"http:\\\/\\\/www.domain.com\\\/catalogsearch\\\/result\\\/index\\\/?ajax=1&brand=Nemox&q=ice+machine\",\"is_selected\":false},{\"label\":\"Manitowoc\",\"count\":6,\"url\":\"http:\\\/\\\/www.domain.com\\\/catalogsearch\\\/result\\\/index\\\/?ajax=1&brand=Manitowoc&q=ice+machine\",\"is_selected\":false},{\"label\":\"Polar Refrigeration\",\"count\":5,\"url\":\"http:\\\/\\\/www.domain.com\\\/catalogsearch\\\/result\\\/index\\\/?ajax=1&brand=Polar+Refrigeration&q=ice+machine\",\"is_selected\":false},{\"label\":\"Longo & Co\",\"count\":4,\"url\":\"http:\\\/\\\/www.domain.com\\\/catalogsearch\\\/result\\\/index\\\/?ajax=1&brand=Longo+%26+Co&q=ice+machine\",\"is_selected\":false},{\"label\":\"Beaumont\",\"count\":3,\"url\":\"http:\\\/\\\/www.domain.com\\\/catalogsearch\\\/result\\\/index\\\/?ajax=1&brand=Beaumont&q=ice+machine\",\"is_selected\":false}]}}}}}\n<\/script>\n\n<\/div>\n                    <\/div>\n                                                                                                                                                                                                                                                                                        <div data-role=\"collapsible\" class=\"filter-options-item\">\n                        <div data-role=\"title\" class=\"filter-options-title\">Power<\/div>\n                        <div data-role=\"content\" class=\"filter-options-content\"><div data-bind=\"scope: 'power_ddFilter'\">\n    <!-- ko template: getTemplate() --> <!-- \/ko -->\n<\/div>\n\n<script type=\"text\/x-magento-init\">\n    {\"*\" : {\"Magento_Ui\/js\/core\/app\": {\"components\": {\"power_ddFilter\": {\"component\":\"Smile_ElasticsuiteCatalog\\\/js\\\/attribute-filter\",\"maxSize\":10,\"displayProductCount\":true,\"hasMoreItems\":false,\"ajaxLoadUrl\":\"http:\\\/\\\/www.domain.com\\\/catalog\\\/navigation_filter\\\/ajax\\\/?ajax=1&filterName=power_dd&q=ice+machine\",\"items\":[{\"label\":\"13 Amp (Plug)\",\"count\":111,\"url\":\"http:\\\/\\\/www.domain.com\\\/catalogsearch\\\/result\\\/index\\\/?ajax=1&power_dd=13+Amp+%28Plug%29&q=ice+machine\",\"is_selected\":false},{\"label\":\"1 Phase (Hard Wired)\",\"count\":2,\"url\":\"http:\\\/\\\/www.domain.com\\\/catalogsearch\\\/result\\\/index\\\/?ajax=1&power_dd=1+Phase+%28Hard+Wired%29&q=ice+machine\",\"is_selected\":false}]}}}}}\n<\/script>\n\n<\/div>\n                    <\/div>\n                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <div data-role=\"collapsible\" class=\"filter-options-item\">\n                        <div data-role=\"title\" class=\"filter-options-title\">Price<\/div>\n                        <div data-role=\"content\" class=\"filter-options-content\"><div class=\"smile-es-range-slider\" data-role=\"range-price-slider-price\">\n    <div data-role=\"from-label\"><\/div>\n    <div data-role=\"to-label\"><\/div>\n    <div data-role=\"slider-bar\"><\/div>\n    <div class=\"actions-toolbar\">\n        <div data-role=\"message-box\"><\/div>\n        <div class=\"actions-primary\">\n            <a class=\"action primary small\" data-role=\"apply-range\">\n                <span>OK<\/span>\n            <\/a>\n        <\/div>\n    <\/div>\n<\/div>\n\n<script type=\"text\/x-magento-init\">\n    { \"[data-role=range-price-slider-price]\" : { \"rangeSlider\" : {\"minValue\":1,\"maxValue\":6091,\"currentValue\":{\"from\":1,\"to\":6091},\"fieldFormat\":{\"pattern\":\"\\u00a3%s\",\"precision\":2,\"requiredPrecision\":2,\"decimalSymbol\":\".\",\"groupSymbol\":\",\",\"groupLength\":3,\"integerRequired\":false},\"intervals\":[{\"value\":1,\"count\":1},{\"value\":2,\"count\":1},{\"value\":3,\"count\":1},{\"value\":40,\"count\":1},{\"value\":60,\"count\":1},{\"value\":64,\"count\":1},{\"value\":150,\"count\":1},{\"value\":179,\"count\":1},{\"value\":190,\"count\":1},{\"value\":242,\"count\":1},{\"value\":291,\"count\":1},{\"value\":325,\"count\":1},{\"value\":355,\"count\":2},{\"value\":395,\"count\":1},{\"value\":465,\"count\":1},{\"value\":472,\"count\":1},{\"value\":515,\"count\":1},{\"value\":520,\"count\":1},{\"value\":535,\"count\":1},{\"value\":555,\"count\":1},{\"value\":577,\"count\":1},{\"value\":585,\"count\":1},{\"value\":599,\"count\":1},{\"value\":605,\"count\":2},{\"value\":615,\"count\":1},{\"value\":640,\"count\":1},\"value\":658,\"count\":1}],\"urlTemplate\":\"http:\\\/\\\/www.domain.com\\\/catalogsearch\\\/result\\\/index\\\/?ajax=1&price=<%- from %>-<%- to %>&q=ice+machine\",\"messageTemplates\":{\"displayCount\":\"<%- count %> products\",\"displayEmpty\":\"No products in the selected range.\"},\"rate\":1} 
                                } }
                            <\/script>
                        <\/div>
            <\/div>
        <\/div>
    <\/div>
<\/div>

只需通过ajax请求即可,然后像这样替换旧的侧边栏:

$(this.options.layeredNavigationFilterBlock).replaceWith(content.filters);
$("#layered-filter-block").applyBindings();

applyBindings似乎使整个补充工具栏变得生动起来,补充工具栏中的所有新过滤器都可以正常使用。以前的边栏中已经存在的某些过滤器但是不会更新,并且与以前的过滤器相同。该如何解决?

使它起作用的唯一方法是重命名绑定和脚本,以在发生这种情况并且加载新内容的情况下进行匹配,但是更简单一些。

0 个答案:

没有答案