具有计算可观察性的无法排序的淘汰赛

时间:2014-03-18 21:07:54

标签: javascript jquery jquery-ui knockout.js knockout-sortable

jsfiddle example。就像标题所说我试图使用计算的observable以及rniemeyer knockout可排序的例子。我一直在

  

需要实现write方法

此错误可在开发者控制台中查看。

我的ko.computed上有一个写方法工具,但它仍然出错。我做错了什么?

下面的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());

1 个答案:

答案 0 :(得分:0)

如果您想要两全其美,那么这里是一个“writableComputedArray”。如果您从数组中添加/删除,并且后续重新计算observable执行相同的添加/删除,则订阅者将不会第二次收到通知。但是,您有责任确保阵列的计算与实际添加/删除的内容之间不存在差异。您可以通过在可排序绑定的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;
};