Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to manually trigger a bindingHandler?
    primarykey
    data
    text
    <p>I have the following bindinghandler in my DOM: </p> <pre><code>&lt;textarea data-bind="value: responseText, pagedown: liveEditors['editor'], valueUpdate: 'afterkeydown'"&gt;&lt;/textarea&gt; </code></pre> <p>Binding handler js: </p> <pre><code>ko.bindingHandlers.pagedown = { init: function (element, valueAccessor, allBindingsAccessor, viewModel) { // when formatting is not enabled, this will be null if (valueAccessor() != null) { valueAccessor().hooks.chain("onPreviewRefresh", function () { $(element).change(); }); } } }; </code></pre> <p>This binding handler is for pagedown formatting. A moment ago, the formatting was displayed by default, so the <code>valueAccessor()</code> was always hit, as the <code>liveEditors['editor']</code> array item was non-null. </p> <p>But now, I'm trying to stop this bindinghandler from running until a condition is met - when the user triggers "showFormatting", I'd like this bindingHandler to re-evaluate, as the array will have been populated. </p> <p>Currently, the bindingHandler is hit one time on load, <code>valueAccessor()</code> is null (as expected - formatting is not enabled), but then it never gets hit again.</p> <p>How can I manually trigger the handler from a function, or pass it an observable to watch before re-evaluating?</p> <p>Update: Changed textarea: </p> <pre><code> &lt;textarea data-bind="value: responseText, pagedown: { editor: liveEditors['editor'], showFormatting: showResponseFormatting }, valueUpdate: 'afterkeydown'"&gt;&lt;/textarea&gt; &lt;a class="response-button-container" data-bind="click: showFormatting"&gt;show editor&lt;/a&gt; </code></pre> <p>JS:</p> <pre><code>self.showFormatting = function() { self.showResponseFormatting(true); buildEditor(); runEditor(); }; self.showResponseFormatting = ko.observable(false); </code></pre> <p>Binding Handler: </p> <pre><code>ko.bindingHandlers.pagedown = { init: function (element, valueAccessor, allBindingsAccessor, viewModel) { var options = valueAccessor(); var showFormatting = ko.unwrap(options.showFormatting); var editor = options.editor; // when formatting is not enabled, this will be null if (showFormatting &amp;&amp; editor != null) { editor.hooks.chain("onPreviewRefresh", function () { $(element).change(); }); } } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload