Skip to content

Instantly share code, notes, and snippets.

@JeffGos
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffGos/3eef4eb84451c9591897 to your computer and use it in GitHub Desktop.
Save JeffGos/3eef4eb84451c9591897 to your computer and use it in GitHub Desktop.
Knockoutjs observableArray with null properties in array items
<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