我需要在文档底部添加文本“列表已更新!”如果用户更新了可排序列表的顺序。
你能否给我提示如何正确地以淘汰赛的方式做到这一点?
var Task = function(name) {
this.name = ko.observable(name);
}
var ViewModel = function() {
var self = this;
self.tasks = ko.observableArray([
new Task("Get dog food"),
new Task("Fix car"),
new Task("Fix fence"),
new Task("Walk dog"),
new Task("Read book")
]);
};
ko.applyBindings(new ViewModel());
div {
padding: 5px;
margin: 5px;
border: black 1px solid;
}
ul {
padding-bottom: 10px;
}
.container {
float: left;
width: 150px;
background-color: #ffd;
}
.item {
background-color: #DDD;
cursor: move;
text-align: center;
}
#results {
float: left;
width: 150px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>
<script src="https://rawgit.com/rniemeyer/knockout-sortable/master/build/knockout-sortable.js"></script>
<script src="https://rawgit.com/rniemeyer/knockout-sortable/master/build/knockout-sortable.js"></script>
<div class="container" data-bind="sortable: tasks">
<div class="item">
<a href="#" data-bind="text: name"></a>
</div>
</div>
<div id="results">
<ul data-bind="foreach: tasks">
<li data-bind="text: name"></li>
</ul>
</div>
答案 0 :(得分:1)
您可以通过订阅可观察数组和新的observable来跟踪通知的可见状态。
b = { << 10 {a[0:5]}}; // EDIT: Correcting typo
然后在任务元素下添加带有if或visible绑定的通知。
self.isUpdated = ko.observable(false);
self.tasks.subscribe(function(value){
self.isUpdated(true);
});
修改:Fiddle
答案 1 :(得分:1)
添加afterMove
处理程序以更新“已更新”标记。然后使用该标志显示您的消息。
如,
<div class="container" data-bind="sortable: { data: tasks,
afterMove: setIsUpdated.bind($root) }">
var ViewModel = function() {
this.tasks = ko.observableArray([
new Task("Get dog food"),
new Task("Fix car"),
new Task("Fix fence"),
new Task("Walk dog"),
new Task("Read book")
]);
this.isUpdated = ko.observable(false);
};
ViewModel.prototype.setIsUpdated = function () {
this.isUpdated(true);
};