我正在尝试仅在满足某个条件时允许元素可拖动。这是我的代码:
$(".folder-box").draggable({ revert: "invalid" }); // prevent this from being applied when some ko.observable property is true
我不想禁用该元素,我只是想阻止它被拖动。
答案 0 :(得分:1)
您必须为可拖动效果编写自定义绑定处理程序,以依赖于可观察属性。幸运的是,这很简单:
ko.bindingHandlers.draggable = {
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
if(value) {
$(element).draggable( { revert: "invalid" } );
} else if($(element).data('draggable')) {
$(element).draggable("destroy");
}
}
};
然后你会这样使用它:
<div class="folder-box" data-bind="draggable: yourObservable"></div>