knockout-else
else/elseif binding for Knockout
Re. https://github.com/knockout/knockout/issues/962
Originally from http://jsfiddle.net/bmh_ca/hyrvtps4/
Get started
Get it with npm install knockout-else
or bower install knockout-else
.
Include the dist/knockout-else.js
, then init with KnockoutElse.init([spec={}])
.
It should work fine in the ordinary with AMD/CommonJS.
How to use
Two block elements
<div data-bind='if: x'>X</div>
<div data-bind='else'>!X</div>
Two virtual elements
X is truthy
X is not truthy.
An Element + virtual else
<div data-bind='template: {if: x, foreach: arr}'></div>
X is false or arr is empty/undefined.
A virtual if and else-element
<div data-bind='else'>
arr is empty or undefined.
</div>
else and else-if
X is true
X is not true, but Y is.
<div data-bind='elseif: z'>Z, but not X nor Y.</div>
It's all pie.
inline else/else-if short-hand
Within an if
, ifnot
and foreach
bindings, one can use HTML comments to indicate else
and elseif
. These are respectively rewritten as <!--/ko--><!--ko else-->
and <!--/ko--><!-- ko elseif: expression -->
.
You need to pass inlineElse: true
to the init
function to turn this feature on.
You can use inline short-hand with regular tags:
<div data-bind='if: x' -->
X
not X but Y
Neither X nor Y
</div>
... and with virtual elements:
X
Y
~X and ~Y
The else
/elseif
comments will be rewritten as:
X
Y
~X and ~Y
The else
and elseif
bindings then add inner conditionals, resulting in a DOM tree that looks like this:
X
Y
~X and ~Y
The __elseCondition__
is a computed value that is true when the prior conditions are false and, in the case of elseif
the given conditional is true.
The "inline else/if" short-hand replaces the knockout
bindings for if
, ifnot
, foreach
and template
.
The original bindings if
, ifnot
, template
and foreach
are
accessible still through ko.bindingHandlers.__ko_BINDING
e.g. ko.bindingHandlers.__ko_if
,
spec
argument for init
You likely do not need to specify any, but the options are:
Argument | Default | Meaning |
---|
inlineElse | false | <bool> When truthy the <!-- else --> and <!-- elseif: --> short-hands will be enabled. |
elseBindingName | else | <string> or falsy The name of the binding for 'else'; falsy to disable. |
elseIfBindingName | elseif | <string> or falsy The name of the binding for 'elseif'; falsy to disable. |
ES5 things
The following will need to be shimmed for older browser support (i.e. IE8 and lower):
Changelog
- 6 Dec 2015 🌵 — Merge #3 — Fix memory leak