Skip to content

Instantly share code, notes, and snippets.

@jsjohnst
Created May 27, 2010 02:32
Show Gist options
  • Save jsjohnst/415367 to your computer and use it in GitHub Desktop.
Save jsjohnst/415367 to your computer and use it in GitHub Desktop.
function callAPI(url, callback) {
var request = new URL();
request.callback = callback;
request.location = url;
KONtx.utility.WaitIndicator.up();
request.fetchAsync(function (u) {
KONtx.utility.WaitIndicator.down();
if (u.response == 200){
KONtx.application.setNetworkRequestFailed(false);
u.callback(u.result);
} else {
KONtx.application.setNetworkRequestFailed(true);
}
});
}
// This example assumes you are following the code pattern
// set forth in our best practices guide.
//
// If you are not, then you need to just subscribe your method
// you created like you would for anything else in the framework
EventHandlers.onNetworkHideDialog.subscribeTo(
KONtx.application,
"onNetworkHideDialog",
EventHandlers
);
if (event.payload.type == 1) {
// Check if you have the base data you need to render your main view
// This would normally be config data which you would fetch at widget startup
// DO NOT fetch data inside this handler, the network is unstable
if (data_ready == false) {
event.preventDefault();
}
}
// Replace myWidget with your widget's custom object
myWidget.network = (function () {
var setDownByUser = false;
return {
setNetworkRequestFailed: function (status) {
KONtx.application.setNetworkRequestFailed(status);
setDownByUser = status;
},
isPhysicalNetworkDown: function () {
if (KONtx.application.getNetworkDownStatus) {
return !setDownByUser;
}
return false;
}
};
})();
if (typeof KONtx.application.isPhysicalNetworkDown === "undefined") {
KONtx.application.isPhysicalNetworkDown = function () {
// Replace myWidget with your widget's custom object
return myWidget.network.isPhysicalNetworkDown();
};
}
function callAPI(url, callback){
var request = new URL();
request.callback = callback;
request.location = url;
KONtx.utility.WaitIndicator.up();
request.fetchAsync(function (u) {
KONtx.utility.WaitIndicator.down();
if (u.response == 200) {
myWidget.network.setNetworkRequestFailed(false);
u.callback(u.result);
} else {
myWidget.network.setNetworkRequestFailed(true);
}
});
}
if (KONtx.application.isPhysicalNetworkDown()) {
log("the network is down!");
return;
}
function callAPI(url, callback) {
if (KONtx.application.isPhysicalNetworkDown()) {
log("the network is down!");
return;
}
var request = new URL();
request.callback = callback;
request.location = url;
KONtx.utility.WaitIndicator.up();
request.fetchAsync(function (u) {
KONtx.utility.WaitIndicator.down();
if (u.response == 200){
myWidget.application.setNetworkRequestFailed(false);
u.callback(u.result);
} else {
myWidget.application.setNetworkRequestFailed(true);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment