Is there a shorter/cleaner way to do the null/undefined testing?
<select data-bind="options: SelectedBusinessLine() ? SelectedBusinessLine().Clusters() : [],
optionsText: 'Title',
value: SelectedCluster,
optionsCaption: 'Select Cluster..'">
</select>
Instead of
data-bind="options: SelectedBusinessLine() ? SelectedBusinessLine().Clusters() : [],
i would like
data-bind="options: SelectedBusinessLine().Clusters(),
give or take the ()
Or at least a simpler null operator check '??' SelectedBusinessLine ?? []
Or a binding param to auto check for null or silent fail.
Any ideas if this is possible?
The "With" works (probably the others works too...)
But with the "With" the role UI disappear/appear even if there are conditions inside... For Example... I need to set a Status (true/false) button, but only if the Status isn't null...
This works. In this case.
But sometimes just the "With" works like Simon_Weaver said!
Most of the above solutions didn't work for me quite out of the box, since I only wanted to apply this to a single element in a
foreach
so I modified the accepted answer's approach a bit:Most of these solutions do not work in a certain case for me, where I am setting an attribute:
The
template
andwith
bindings would not work here, because if I did not have an active descendant, then my div would be empty. For my solution, I created an observable method:Which allows me to change my binding to this:
One way not mentioned in the otherwise excellent page referenced by another answer is to use
with
This whole UI will disappear if
selecteditem
is null.I prefer this method
Create a custom binding
Usage
The only extra stuff you need to add is to wrap your original value with 'function() { return ... }'
This however, will stop all errors beneath the value call. You could improve the custom binding by only looking for 'undefined' exceptions. You may also like to improve this binding by adding in a default text option.
This page provides several solutions. The relevant part is this one:
Protecting against null objects
If you have an observable that contains an object and you want to bind to properties of that object, then you need to be careful if there is a chance that it can be null or undefined. You may write your binding like:
There are a number of ways to handle this one. The preferred way would be to simply use the template binding:
With this method, if
selectedItem
is null, then it just won’t render anything. So, you would not see unknown as you would have in the original binding. However, it does have the added benefit of simplifying your bindings, as you can now just specify your property names directly rather thanselectedItem().name
. This is the easiest solution.Just for the sake of exploring some options, here are a few alternatives:
You could use a computed observable, as we did before.
However, this again adds some bloat to our view model that we may not want and we might have to repeat this for many properties.
You could use a custom binding like:
Is this better than the original? I would say probably not. It does avoid the JavaScript in our binding, but it is still pretty verbose.
One other option would be to create an augmented observable that provides a safe way to access properties while still allowing the actual value to be null. Could look like:
So, this is just an observable that also exposes a computed observable named safe that will always return an empty object, but the actual observable can continue to store null.
Now, you could bind to it like:
You would not see the unknown value when it is null, but it at least would not cause an error when
selectedItem
is null.I do think that the preferred option would be using the template binding in this case, especially if you have many of these properties to bind against.