最新的敲除和jquery ui对话框出错:无法在初始化之前调用 [英] Error with latest knockout and jquery ui dialog: cannot call prior to initialization
问题描述
我尝试与敲除2.2自定义绑定一起使用新的jQuery 1.9和jQuery ui 1.9.2. 代码来自此处:将jquery ui对话框与敲除js集成 使用更新的库: http://jsfiddle.net/SnPdE/323/
I try to use with knockout 2.2 custom binding new jquery 1.9 and jquery ui 1.9.2. Code is from here: integrating jquery ui dialog with knockoutjs With updated libraries: 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);
问题是错误:错误:初始化前无法在对话框上调用方法;尝试调用方法关闭"
Problem is error: Error: cannot call methods on dialog prior to initialization; attempted to call method 'close'
如果我删除setTimeout-applyBindings将两次应用于对话框.
If I remove setTimeout - applyBindings is applyed to dialog two times.
推荐答案
在调用open
之前检查对话框是否已初始化将对其进行修复.
Checking if the dialog is initialized before calling open
will fix it.
if ($(element).data('dialog')) {
$(element).dialog(shouldBeOpen ? "open" : "close");
}
根本不需要初始update
,因为如果autoOpen
为true
(默认值),则会在初始化期间打开对话框.
The initial update
is not required at all, as the dialog will be opened during initialization if autoOpen
is true
, which is the default.
修改:
为了使dialogVisible
最初为false
时正确无误,应进行更改以设置autoOpen
选项.
To be correct when the dialogVisible
is initially false
a change should be made to set the autoOpen
option.
...
options.autoOpen = ko.utils.unwrapObservable(allBindingsAccessor().dialogVisible);
$(element).dialog(options);
这篇关于最新的敲除和jquery ui对话框出错:无法在初始化之前调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!