Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3efe66
commit d341bcb
Showing
4 changed files
with
185 additions
and
70 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,39 @@ | ||
import { | ||
unwrap, dependencyDetection | ||
unwrap | ||
} from 'tko.observable' | ||
|
||
import ConditionalBindingHandler from './ConditionalBindingHandler' | ||
|
||
import { | ||
cloneNodes, virtualElements | ||
} from 'tko.utils' | ||
|
||
import { computed } from 'tko.computed' | ||
import { | ||
applyBindingsToDescendants, AsyncBindingHandler | ||
bindingContext, inheritParentIndicator | ||
} from 'tko.bind' | ||
|
||
/** | ||
* ⁉️ ⁉️ ⁉️ ⁉️ ⁉️ ⁉️ | ||
* Why this works and the other does not is a problematic quagmire to debug. | ||
* | ||
* If you look at _evalIfChanged in computed.js, it's called 9 times for this | ||
* version, but only 4 for the other version. | ||
*/ | ||
export class WithBindingHandler extends AsyncBindingHandler { | ||
constructor (...args) { | ||
super(...args) | ||
const element = this.$element | ||
let savedNodes | ||
|
||
this.computed(() => { | ||
const shouldDisplay = !!unwrap(this.value) | ||
const isFirstRender = !savedNodes | ||
|
||
// Save a copy of the inner nodes on the initial update, but only if we have dependencies. | ||
if (isFirstRender && dependencyDetection.getDependenciesCount()) { | ||
savedNodes = cloneNodes(virtualElements.childNodes(element), true /* shouldCleanNodes */) | ||
} | ||
|
||
if (shouldDisplay) { | ||
if (!isFirstRender) { | ||
virtualElements.setDomNodeChildren(element, cloneNodes(savedNodes)) | ||
} | ||
applyBindingsToDescendants( | ||
this.$context.createChildContext(this.valueAccessor), | ||
element) | ||
} else { | ||
virtualElements.emptyNode(element) | ||
} | ||
}) | ||
} | ||
|
||
get controlsDescendants () { return true } | ||
static get allowVirtualElements () { return true } | ||
} | ||
import ConditionalBindingHandler from './ConditionalBindingHandler' | ||
|
||
/** | ||
* The following fails somewhere in the `limit` functions of Observables i.e. | ||
* it's an issue related to async/deferUpdates. | ||
*/ | ||
export class WithBindingHandlerFailsInexplicably extends ConditionalBindingHandler { | ||
export class WithBindingHandler extends ConditionalBindingHandler { | ||
constructor (...args) { | ||
super(...args) | ||
this.asOption = this.allBindings.get('as') | ||
|
||
// If given `as`, reduce the condition to a boolean, so it does not | ||
// change & refresh when the value is updated. | ||
const conditionalFn = this.asOption | ||
? () => Boolean(unwrap(this.value)) : () => unwrap(this.value) | ||
this.conditional = this.computed(conditionalFn) | ||
|
||
this.computed('render') | ||
} | ||
|
||
get bindingContext () { | ||
return this.$context.createStaticChildContext(this.valueAccessor) | ||
return this.asOption | ||
? this.$context.extend({[this.asOption]: this.value}) | ||
: this.$context.createChildContext(this.valueAccessor) | ||
} | ||
|
||
renderStatus () { | ||
if (typeof this.value === 'function') { this.value() } // Create dependency | ||
const shouldDisplay = !!unwrap(this.value) | ||
return {shouldDisplay} | ||
const shouldDisplay = Boolean(this.conditional()) | ||
return { shouldDisplay } | ||
} | ||
} |
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
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