Ok, I put this at the bottom of my init.js after the framework has been loaded:
CODE
if (typeof KONtx.application.isPhysicalNetworkDown === "undefined") {
(function () {
//first reassign the original function before deleting it; it's funky b/c it's a setter;
var original = KONtx.application.setNetworkRequestFailed,
setDownByUser = false;
delete KONtx.application.setNetworkRequestFailed;
KONtx.application.setNetworkRequestFailed = function (status) {
original(status);
setDownByUser = status;
};
KONtx.application.isPhysicalNetworkDown = function () {
if (KONtx.application.getNetworkDownStatus) {
return !setDownByUser;
}
return false;
};
}());
}
So, if it detects that KONtx.application.isPhysicalNetworkDown is undefined, it will define that and redefine KONtx.application.setNetworkRequestFailed, storing the original setter definition in the closure created by the self-executing function.
Then, in my API call (there's only one in my widget), it's business as usual:
CODE
if (KONtx.application.isPhysicalNetworkDown()) {
log("Network is down, don't allow any xhr requests!");
return;
}
var xhr = new XMLHttpRequest();
xhr.open("GET", o.url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
o.success(xhr);
KONtx.application.setNetworkRequestFailed(false);
} else {
log("The xhr request failed.");
o.error();
KONtx.application.setNetworkRequestFailed(true);
}
}
};
xhr.send(null);
The important thing here is that I still call KONtx.application.setNetworkRequestFailed to set the status of the network request, and I don't have to worry about creating a custom object and referencing that like has been noted in the docs. In other words, once I include that code in init.js, I don't need to be concerned anymore with whether the framework supports isPhysicalNetworkDown, as I've defined it in the case that it doesn't.
Also, I find it really useful to use a proxy server when testing this in the wdk. We use polipo, and here are some steps to install it and use it:
CODE
#install polipo proxy
sudo apt-get install polipo
#lanch konfabulator with a proxy
KF_PROXY=http://localhost:8123 Konfabulator
#in another shell
#bring down proxy
sudo /etc/init.d/polipo stop
#the uplink is now down, test away
#bring the uplink back up
sudo /etc/init.d/polipo start
#rinse and repeat
Lastly, this patch should be included as long as there are frameworks in the field that don't natively support isPhysicalNetworkDown. Also, I'm going to see to it that our docs (FAQ and blog post) are updated as soon as I can.
- Ben