简体   繁体   English

更改可观察数组会更改自定义绑定处理程序的敲除可见性

[英]Changing observable array alters visibility of custom binding handler, knockoutjs

Update: JSFiddle http://jsfiddle.net/OrganicCat/CjH87/6/ 更新: JSFiddle http://jsfiddle.net/OrganicCat/CjH87/6/

I have an area that is populated by a normal observable array, and when a button is clicked, an asynchronous service call is made that repopulates that array, but with more data. 我有一个由普通可观察数组填充的区域,单击按钮后,将进行异步服务调用,以重新填充该数组,但包含更多数据。

This also causes a hidden dom element to display and displays some of that array data there. 这还会导致显示一个隐藏的dom元素,并在其中显示该数组数据中的某些数据。

Everything works fine until the observable array data is updated, it closes the hidden dom element. 一切正常,直到可观察的数组数据更新,它关闭了隐藏的dom元素。 It is this event inside Knockout.js(library) that triggers it: 触发它的是Knockout.js(library)中的此事件:

// Ignore writes if the value hasn't changed
    if ((!observable['equalityComparer']) || !observable['equalityComparer'](_latestValue, arguments[0])) {
    observable.valueWillMutate();
    _latestValue = arguments[0];
    if (DEBUG) observable._latestValue = _latestValue;
    observable.valueHasMutated(); // This event HERE

Is there a way to prevent this from closing the custom binding? 有没有办法防止这种情况关闭自定义绑定? Could anything in the binding be causing this? 绑定中的任何内容可能会导致这种情况吗? Here is my custom binding handler: 这是我的自定义绑定处理程序:

ko.bindingHandlers.expandAmenities = {
                init: function (element) {
                    $('.expandable.closed').hide();
                    $('.itineraryRowMain .t-go .toggle-expand-rowAmenities').unbind('click').on('click', function (e) {

                        var $itin_body = $(this).closest('.module-admin-group');
                        if ($itin_body.hasClass('closed')) {
                            $(this).parent().parent().next().show();
                            self.bindAmenities(); // Bind amenity details on open
                            //$(this).children().html('-');
                        } else {
                            $(this).parent().parent().next().hide();
                            //$(this).children().html('+');
                        }
                        $itin_body.toggleClass('open closed');
                    });
                }
            };

To summarize, the expand area has a clickable element that will show more data. 总而言之,扩展区域具有可单击的元素,它将显示更多数据。 When this updates the array (just a plain old self.listofStuff(arr);) this causes the new area to get hidden again. 当这更新数组时(只是一个普通的旧self.listofStuff(arr);),这将导致新区域再次被隐藏。

I've figured out the answer. 我已经找到答案了。 So the problem is that if you bind an array that generates DOM elements (like a list or whatever) and you have elements inside that array that you want to update without doing a .push and without resetting the entire array with self.myArray(newArray) then you have to use an observable variable within the array and update that. 因此,问题在于,如果您绑定一个生成DOM元素的数组(如列表或其他内容),并且该数组中有要更新的元素,而无需执行.push且无需使用self.myArray(newArray ),则必须在数组中使用可观察变量并对其进行更新。

When you modify the observable, it will not redraw the entire array, thereby keeping your dynamically altered elements (like visible/hidden divs within the DOM array) in the same state if they were altered by jQuery or whatever. 当您修改可观察对象时,它不会重绘整个数组,从而使动态更改的元素(例如DOM数组中的可见/隐藏的div)保持相同的状态(如果通过jQuery或其他方式对其进行了更改)。

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

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