SteveSanderson/knockout — Issue #367: Reconsider Virtual Elements
Author: troyji
Created: 2012-03-07 16:35:10+00:00
State: open

### The Issue

Knockout now supports the idea of virtual elements through comments. I propose that an alternate implementation be considered and the current one be obsoleted. Below are my thoughts on the matter.

```html

someFlag is true!

```

In my mind, this is not an ideal solution for a variety of reasons. Here are a couple:

* Comments should be just that, comments. Programmers routinely overlook comments as insignificant text in the programming logic, as they are intended. However, the use of this new feature means that comments may contain significant programming logic that must be considered. This goes against common standards, and encourages knockout users to use this arguably bad practice. You generally only see this sort of thing in hacks where no alternatives are possible.

* Depending on the concept outlined in the previous point, some tools ignore or even transform comments. The use of such tools is no longer possible when using this new feature.

### A Trivial Alternative
A trivial alternative would be to introduce an omit binding that would signify the host element is always removed from the DOM and its children are inserted into its parent.

```html

someFlag is true!

```

### A Better Alternative, But A Breaking Change

The primary reason to use the virtual elements is for flow control (we don't want to insert elements to accomplish this). An ideal solution, though breaking, would be to have the flow control bindings also work on the host element, not just the child elements. In other words, an element using an 'if' binding would be removed from the DOM when it evaluates to false, not just its children. This would require that flow control bindings are evaluated ahead of other bindings.

### An Ideal Alternative

To avoid a breaking change, new bindings could be introduced (e.g. 'useIf', 'useIfNot', 'useWith', 'useForeach').

```html

First

Last

```