Skip to content

Instantly share code, notes, and snippets.

@kyleondata
Created July 19, 2012 18:54
Show Gist options
  • Save kyleondata/3145973 to your computer and use it in GitHub Desktop.
Save kyleondata/3145973 to your computer and use it in GitHub Desktop.
Knockout null reference example
// Makes the call
$.ajax({
url: "<SOME URL TO QUERY>",
dataType: "json",
cache: false
}).error(function (jqXHR, textStatus) {
}).success(function (data) {
// Loop through each returned result
for (var key in data) {
// Loop through each property of each returned result
for (var ckey in data[key]) {
// Is it the problem property?
if (ckey == "<WHAT EVER PROP YOU WANT TO CHECK>") {
// For example if we try to access data[0].Customer.Name and customer is null
// It will blow up knockout.js so lets do some fancy stuffs!
if (data[key][ckey] == null){
// Just simply set it from null to empty string.
data[key][ckey] = "";
}
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment