Last active
August 29, 2015 14:05
-
-
Save JeffGos/3eef4eb84451c9591897 to your computer and use it in GitHub Desktop.
Knockoutjs observableArray with null properties in array items
This file contains 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
<table> | |
<thead> | |
<tr> | |
<th>Name</th> | |
<th>Something that could be NULL</th> | |
</tr> | |
</thead> | |
<tbody data-bind="foreach: valuesArray"> | |
<tr> | |
<td data-bind="text: Name"></td> | |
<td data-bind="safeText: Nullable"></td> | |
</tr> | |
</tbody> | |
</table> | |
<script type="text/javascript"> | |
ko.bindingHandlers.safeText = { | |
update: function (element, valueAccessor, allBindingsAccessor) { | |
var text; | |
try { | |
text = ko.utils.unwrapObservable(valueAccessor()); | |
} catch (e) { | |
text = ""; | |
} | |
ko.bindingHandlers.text.update(element, function () { return text; }); | |
} | |
}; | |
var viewModel = { | |
valuesArray : ko.observableArray([]) | |
}; | |
ko.applyBindings(viewModel); | |
viewModel.valuesArray.push({Name: "no null value", Nullable: "NOT NULL"}); | |
viewModel.valuesArray.push({Name: "has a null value"}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment