Reopen an existing Kendo window.
Apr 19 |
Type in "Cannot call method 'destroy' of kendoWindow before it is initialized" into Google, and you won't find a single result. You'll find many other topics, but not one that matches this particular error message. Here is a solution on how I overcame this error, and why I think that it may have happened in the first place. My personal website uses a Greensock animation carriage. It is my guess that the errors are due to having a lot of subtle animations there, and it is resource-intensive as it has extensive scroll and touch listeners. Along with the Kendo map widget, the site is very resource-intensive on the client end. This is probably freezing the destroy methods that are used to typically close a Kendo window. Whatever the actual reason, when I try to destroy the window that contains the map, as I usually do, I run into multiple errors and the window will not open again. My approach was to build a function that would check if the window was previously opened, and then closed, to simply re-open it and then refresh the content (it has dynamic content). If the window was not already created (by someone opening it), then I would create the window, as usual, but I eliminated Kendo's destroy method when the window is closed. I don't want to destroy it- in fact- I can't. It causes errors. If the window was opened, and closed (making the window hidden since it does not have the destroy method), I would re-open the existing window.
Here is the code:
function openMapWindow() {
$( document ).ready(function() {
/* This is a totally weird approach to open a kendo window. I have coded kendo windows for several years, and have probably coded several hundred windows, but using a Kendo window in this greensock platform, especially with Kendo maps, just does not work. Here, I am testing to see if the window is:
1) defined (the first mapWindow line of code will error out if the window has not already been opened)
2) and if the window is defined, is it hidden (it will be- I am not destroying the window like I always do in other code as I have problems with that here.)
If both conditions ARE true, then I merely reopen the existing window that was hidden when the user clicked on the 'x' at the top of the window.
If both conditions are NOT true, then I create the window as usual (but I don't destroy it as usual as I had problems there too).
Essentially what I am doing is:
- Creating the window if the window was not first opened and then closed.
- Or reopening the window if it was opened and then closed.
*/
// This must be put into a try block as the 'var mapWindow = $("#mapWindow").data("kendoWindow");' will cause errors if the window has not already bee already opened.
try {
// Get a reference to the opened window.
var mapWindow = $("#mapWindow").data("kendoWindow");
// Determine if the window was closed...
var mapWindowIsHidden = mapWindow.element.is(":hidden"); //returns true or false
// If the window was closed (and now hidden), re-open the existing window.
if (mapWindowIsHidden){
// Change the title
mapWindow.title(getMapDataByScene(sceneIndex));
// Open it.
mapWindow.open();
// And refresh the window.
mapWindow.bind("refresh", window_refresh);
}
// Otherwise create the window (for the first time)...
} catch(e) {
// Initialize the window.
var mapWindow = $('#mapWindow').kendoWindow({
title: getMapDataByScene(sceneIndex),
actions: ["Refresh", <cfif not session.isMobile>"Minimize", </cfif>"Close"],
modal: false,
resizable: true,
draggable: true,
pinned: true, // Note: we must pin this window, or it will open at the top of the page at all times when we use this approach.
position: { top: 100 },
width: getGrizzlyWindowWidth(),
height: getGrizzlyWindowHeight(),
iframe: false, // Don't use iframes unless it is content derived outside of your own site.
content: "/includes/layers/map.cfm?sceneIndex=" + sceneIndex,// Make sure to create an absolute path here. I had problems with a cached page.
<cfif session.isMobile>
animation: {
close: {
effects: "slideIn:right",
reverse: true,
duration: 500
}
}
</cfif>
}).data('kendoWindow').center();// Center the window.
}//..try
});
}//..function openMapWindow() {
Additionally, if you don't need a dynamic window where the content changes, you can simply use the following simple approach:
// Dynamic window for the approval routing picker.
// This window also has an id variable.
// Original inspiration provided by Ona Bai (http://dojo.telerik.com/@OnaBai/ekIba/2).
// To use, first create a div at the top of the page like so: <div id="dynamicDetailWindow"></div>
function dynamicRoutingPickerWindow(id, id2, name, width, height) {
var thisWin = $("#" + name).data("kendoWindow");
if (thisWin) {
thisWin.open();
} else {
thisWin = $("<div id='" + name + "'></div>").kendoWindow({
actions: ["Minimize", "Maximize", "Refresh", "Close"],
title: "Contract Details",
content: "includes/routingPicker.cfm?contractId=" + id,
width: width,
height: height,
modal: false,
resizable: true,
draggable: true,
// Open the window near the top of the page.
position:{
top:"15%",
},
appendTo: "#dynamicRoutingPickerWindow",
visible: true
}).data("kendoWindow").center();
}
}
Related Entries
Tags
Kendo WindowThis entry was posted on April 19, 2019 at 2:15 AM and has received 3263 views.