我尝试使用淘汰赛 2.2 自定义绑定新 jquery 1.9 和 jquery ui 1.9.2。代码来自这里:将 jquery ui 对话框与 knockoutjs 集成 使用更新的库:http: //jsfiddle.net/SnPdE/323/
ko.bindingHandlers.dialog = {
init: function(element, valueAccessor, allBindingsAccessor) {
var options = ko.utils.unwrapObservable(valueAccessor()) || {};
//do in a setTimeout, so the applyBindings doesn't bind twice from element being copied and moved to bottom
setTimeout(function() {
options.close = function() {
allBindingsAccessor().dialogVisible(false);
};
$(element).dialog(options);
}, 0);
//handle disposal (not strictly necessary in this scenario)
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$(element).dialog("destroy");
});
},
update: function(element, valueAccessor, allBindingsAccessor) {
var shouldBeOpen = ko.utils.unwrapObservable(allBindingsAccessor().dialogVisible);
$(element).dialog(shouldBeOpen ? "open" : "close");
}
};
var viewModel = {
label: ko.observable('dialog test'),
isOpen: ko.observable(false),
open: function() {
this.isOpen(true);
},
close: function() {
this.isOpen(false);
}
};
ko.applyBindings(viewModel);
问题是错误:错误:在初始化之前无法调用对话框上的方法;试图调用方法“关闭”
如果我删除 setTimeout - applyBindings 两次应用于对话框。