简体   繁体   English

淘汰赛排序与计算的可观察无法正常工作

[英]knockout sortable with computed observable not working

jsfiddle example . jsfiddle示例 Like the title says I am trying to use a computed observable along with rniemeyer knockout sortable example. 就像标题所说的那样,我正在尝试将计算的可观察值与rniemeyer剔除可排序示例一起使用。 I keep getting 我不断

the write method needs to be implemented 写方法需要实现

This error is viewable in the developer console. 在开发人员控制台中可以看到此错误。

I have a write method implement on my ko.computed but it still errors out. 我在ko.computed上有一个write方法实现,但仍然出错。 What I am I doing wrong? 我做错了什么?

html and javascript below 下面的html和javascript

<div id="main">
    <h3>Tasks</h3>
    <div class="container" data-bind="sortable: tasks">
        <div class="item">
            <span data-bind="visible: !$root.isTaskSelected($data)">
                <a href="#" data-bind="text: TestName"></a>
            </span>
            <span data-bind="visibleAndSelect: $root.isTaskSelected($data)">
                <input data-bind="value: name, event: { blur: $root.clearTask }" />
            </span>  
        </div>
    </div>
</div>

var Task = function(first,last) {
    var self = this;
    self.firstName = ko.observable(first);
    self.lastName = ko.observable(last);
    self.TestName = ko.computed({
        read: function (){
            return self.firstName() + " " +  self.lastName();
        },
        write: function (item) {
            console.log(item);
        }
    });

    return self;
}

var ViewModel = function() {
    var self = this;
    self.testTasks = ko.observableArray([
        new Task("test","one"),
        new Task("test","two"),
        new Task("test","three")
         ]);

    self.tasks = ko.computed({
        read: function() { return self.testTasks();},
        write: function(item) {console.log(item);}
    }); 



    self.selectedTask = ko.observable();
    self.clearTask = function(data, event) {
        if (data === self.selectedTask()) {
            self.selectedTask(null);                
        }

        if (data.name() === "") {
           self.tasks.remove(data);   
        }
    };
    self.addTask = function() {
        var task = new Task("new");
        self.selectedTask(task);
        self.tasks.push(task);
    };

    self.isTaskSelected = function(task) {
       return task === self.selectedTask();  
    };
};

//control visibility, give element focus, and select the contents (in order)
ko.bindingHandlers.visibleAndSelect = {
    update: function(element, valueAccessor) {
        ko.bindingHandlers.visible.update(element, valueAccessor);
        if (valueAccessor()) {
            setTimeout(function() {
                $(element).find("input").focus().select();
            }, 0); //new tasks are not in DOM yet
        }
    }
};

ko.applyBindings(new ViewModel());

As the very author of this plugin says here , you can't use a computed observable; 作为这个插件的非常作者说在这里 ,你不能使用计算观察的; the sortable plugin depends on an actual observable array. sortable插件取决于实际的可观察数组。

Which makes sense when you think about it: the plugin is actually manipulating the various indexes of the array as you re-sort the elements. 当您考虑它时,这是有道理的:当您对元素进行重新排序时,插件实际上是在操纵数组的各种索引。

Here's a "writableComputedArray" if you want the best of both worlds. 如果您想两全其美,这是一个“ writableComputedArray”。 If you add/remove from the array, and a subsequent re-compute of the observable performs the same add/remove, subscribers will not get notified the second time. 如果您从数组中添加/删除,并且随后对observable的重新计算执行相同的添加/删除,则订阅服务器将不会第二次收到通知。 However, it's your responsibility to make sure that there are no discrepancies between the computation of the array and what actually gets added/removed. 但是,您有责任确保数组的计算与实际添加/删除的内容之间没有差异。 You can accomplish this by making the necessary changes in the sortable binding's afterMove event. 您可以通过在可排序绑定的afterMove事件中进行必要的更改来完成此操作。

ko.writeableComputedArray = function (evaluatorFunction) {
    // We use this to get notified when the evaluator function recalculates the array.
    var computed = ko.computed(evaluatorFunction);

    // This is what gets returned to the caller and they can subscribe to
    var observableArray = ko.observableArray(computed());

    // When the computed changes, make the same changes to the observable array.
    computed.subscribe(function (newArray) {
        // Add any new values
        newArray.forEach(function (value) {
            var i = observableArray.indexOf(value);

            if (i == -1) {
                // It's a new value, push it
                observableArray.unshift(value);
            }
        });

        // Remove any old ones.  Loop backwards since we're removing items from it.
        for (var valueIndex = observableArray().length - 1; valueIndex >= 0; valueIndex--) {
            var value = observableArray()[valueIndex];

            var i = newArray.indexOf(value);

            if (i == -1) {
                // It's an old value, remove it
                observableArray.remove(value);
            }
        }
    });

    return observableArray;
};

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

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