Created
July 19, 2012 18:54
-
-
Save kyleondata/3145973 to your computer and use it in GitHub Desktop.
Knockout null reference example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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