我正在使用.Net MVC的Kendo Ui控件。我希望能够在javascript中检查控件上是否存在事件。例如,我声明了一个如下对话窗口。在其他地方我声明对话框但不添加刷新事件。如何检查javascript是否存在刷新事件?
@(Html.Kendo().Window()
.Title("Clone Existing Address")
.Name("myDialog")
.Modal(true)
.Draggable()
.Resizable()
.Width(800)
.Visible(false)
.Actions(actions => actions
.Minimize()
.Maximize()
.Close()
)
.Events(e => e.Refresh("refreshDialog"))
)
示例javascript:这个dosnt工作了!!
function refreshEventExists() {
var dialog = $("#myDialog").data("kendoWindow");
if (dialog.refresh) {
alert('Refreh event exists');
}
else {
alert('Refreh event DOES NOT exists');
}
}
答案 0 :(得分:5)
您可以检查widget._events
:
function numberOfHandlers(widget, eventName) {
if (widget._events.hasOwnProperty(eventName)) {
return widget._events[eventName].length;
}
return 0;
}
var dialog = $("#dialog2").kendoWindow().data("kendoWindow");
dialog.bind("activate", function () {});
console.log(numberOfHandlers(dialog, "activate")); // logs "1" (one handler for the activate event)
console.log(numberOfHandlers(dialog, "refresh")); // logs "0" (no handlers for the refresh event
(demo)