# wijmo.knockout.base Module

## Content

<div class="content__tsd">
						<div style="display:flex;justify-content:space-between;align-items:center">
							<h1>
								wijmo.knockout.base Module
							</h1>
						</div>
						<section class="tsd-comment">
							<div class="tsd-comment">
								<div class="lead">
									<p>Contains KnockoutJS bindings for the Wijmo controls.</p>
								</div>
								<p>The bindings allow you to add Wijmo controls to
									<a href="http://knockoutjs.com/" target="_blank">KnockoutJS</a>
								applications using simple markup in HTML pages.</p>
								<p>To add a Wijmo control to a certain place in a page's markup, add the <b>&lt;div&gt;</b>
									element and define a binding for the control in the <b>data-bind</b> attribute.
									The binding name corresponds to the control name with a wj prefix. For example, the <a href="../classes/wijmo_knockout_input.wjinputnumber.html">wjInputNumber</a>
									binding represents the Wijmo <a href="../classes/wijmo_input.inputnumber.html">InputNumber</a> control. The binding value is an object literal containing
									properties corresponding to the control's read-write property and event names, with their values defining
								the corresponding control property values and event handlers.</p>
								<p>The following markup creates a Wijmo <b>InputNumber</b> control with the <b>value</b> property bound to the
								view model's <b>theValue</b> property, the <b>step</b> property set to 1 and the <b>inputType</b> property set to 'text':</p>
								<pre>&lt;div data-bind="wjInputNumber: { value: theValue, step: 1, inputType: 'text' }"&gt;&lt;/div&gt;</pre>
								<h3>Custom elements</h3>
								As an alternative to the standard Knockout binding syntax, the Wijmo for Knockout provides a possibility to declare controls
								in the page markup as custom elements, where the tag name corresponds to the control binding name and the attribute names
								correspond to the control property names. The element and parameter names must be formatted as lower-case with dashes instead
								of camel-case. The control in the example above can be defined as follows using the custom element syntax:
								<pre>&lt;wj-input-number value="theValue" step="1" input-type="'text'"&gt;&lt;/wj-input-number&gt;</pre>
								<p>Note that attribute values should be defined using exactly the same JavaScript expressions syntax as you use in
									data-bind definitions. The Wijmo for Knockout preprocessor converts such elements to the conventional data-bind form,
								see the <b>Custom elements preprocessor</b> topic for more details.</p>
								<h3>Binding to control properties</h3>
								Wijmo binding for KnockoutJS supports binding to any read-write properties on the control. You can assign any
								valid KnockoutJS expressions (e.g. constants, view model observable properties, or complex expressions) to the
								property.
								<p>Note that binding expression should resolve (after calling <b>ko.unwrap(expression)</b> on it) to a pure
									JavaScript value understandable by the corresponding Wijmo JavaScript control. This in particular means
									that you can’t bind the <b>itemsSource</b> property of Wijmo controls to a Knockout <b>observableArray</b>,
								or to array whose items’ properties are Knockout <b>observable</b>(s).</p>
								<p>Most of the properties provide one-way binding, which means that changes in the bound observable view model
									property cause changes in the control property that the observable is bound to, but not vice versa.
									But some properties support two-way binding, which means that changes made in the control property are
									propagated back to an observable bound to the control property as well. Two-way bindings are used for properties
									that can be changed by the control itself, by user interaction with the control,
									or by other occurences. For example, the InputNumber control provides two-way binding for the
									<b>value</b> and <b>text</b> properties, which are changed by the control while a user is typing a new value.
								The rest of the InputNumber properties operate in the one-way binding mode.</p>
								<h3>Binding to control events</h3>
								To attach a handler to a control event, specify the event name as a property of the object literal defining
								the control binding, and the function to call on this event as a value of this property.
								Wijmo bindings follow the same rules for defining an event handler as used for the intrinsic KnockoutJS bindings
								like <b>click</b> and <b>event</b>. The event handler receives the following set of parameters, in the specified order:
								<ul>
									<li><b>data:</b> The current model value, the same as for native KnockoutJS bindings like <b>click</b> and <b>event</b>. </li>
									<li><b>sender:</b> The sender of the event. </li>
									<li><b>args:</b> The event arguments. </li>
								</ul>
								<p>The following example creates an <b>InputNumber</b> control and adds an event handler for the <b>valueChanged</b>
								event showing a dialog with a new control value.</p>
								<pre>&lt;!-- HTML --&gt;
&lt;div data-bind="wjInputNumber: { value: theValue, step: 1, valueChanged: valueChangedEH }"&gt;&lt;/div&gt;
&nbsp;
//View Model
this.valueChangedEH = function (data, sender, args) {
    alert('The new value is: ' + sender.value);
}</pre>
								<p>The same control defined using the custom element syntax:</p>
								<pre>&lt;wj-input-number value="theValue" step="1" value-changed="valueChangedEH"&gt;&lt;/wj-input-number&gt;</pre>
								<h3>Binding to undefined observables</h3>
								View model observable properties assigned to an <i>undefined</i> value get special treatment by Wijmo
								bindings during the initialization phase. For example, if you create an observable as ko.observable(undefined)
								or ko.observable() and bind it to a control property, Wijmo does not assign a value to the control. Instead,
								for properties supporting two-way bindings, this is the way to initialize the observable with the control's
								default value, because after initialization the control binding updates bound observables with the control
								values of such properties. Note that an observable with a <i>null</i> value, e.g. ko.observable(null), gets
								the usual treatment and assigns null to the control property that it is bound to. After the primary
								initialization has finished, observables with undefined values go back to getting the usual treatment from
								Wijmo, and assign the control property with undefined.
								<p>In the example below, the <b>value</b> property of the <b>InputNumber</b> control has its default value of 0
								after initialization, and this same value is assigned to the view model <b>theValue</b> property:</p>
								<pre>&lt;!-- HTML --&gt;
&lt;div data-bind="wjInputNumber: { value: theValue }"&gt;&lt;/div&gt;
&nbsp;
//View Model
this.theValue = ko.observable();</pre>
								<h3>Defining complex and array properties</h3>
								Some Wijmo controls have properties that contain an array or a complex object. For example, the
								<a href="../classes/wijmo_chart.flexchart.html">FlexChart</a> control exposes <b>axisX</b> and <b>axisY</b> properties that represent an <a href="../classes/wijmo_chart.axis.html">Axis</a> object;
								and the <b>series</b> property is an array of <a href="../enums/wijmo_chart.selectionmode.html#series">Series</a> objects. Wijmo provides special
								bindings for such types that we add to child elements of the control element. If the control exposes
								multiple properties of the same complex type, then the <b>wjProperty</b> property of the complex
								type binding specifies which control property it defines.
								<p>The following example shows the markup used to create a <b>FlexChart</b> with <b>axisX</b> and <b>axisY</b>
								properties and two series objects defined:</p>
								<pre>&lt;div data-bind="wjFlexChart: { itemsSource: data, bindingX: 'country' }"&gt;
    &lt;div data-bind="wjFlexChartAxis: { wjProperty: 'axisX', title: chartProps.titleX }"&gt;&lt;/div&gt;
    &lt;div data-bind="wjFlexChartAxis: { wjProperty: 'axisY', title: chartProps.titleY }"&gt;&lt;/div&gt;
    &lt;div data-bind="wjFlexChartSeries: { name: 'Sales', binding: 'sales' }"&gt;&lt;/div&gt;
    &lt;div data-bind="wjFlexChartSeries: { name: 'Downloads', binding: 'downloads' }"&gt;&lt;/div&gt;
&lt;/div&gt;</pre>
								<p>The same control defined using the custom element syntax:</p>
								<pre>&lt;wj-flex-chart items-source="data" binding-x="'country'"&gt;
    &lt;wj-flex-chart-axis wj-property="'axisX'" title="chartProps.titleX"&gt;&lt;/wj-flex-chart-axis&gt;
    &lt;wj-flex-chart-axis wj-property="'axisY'" title="chartProps.titleY"&gt;&lt;/wj-flex-chart-axis&gt;
    &lt;wj-flex-chart-series name="'Sales'" binding"'sales'"&gt;&lt;/wj-flex-chart-series&gt;
    &lt;wj-flex-chart-series name="'Downloads'" binding"'downloads'"&gt;&lt;/wj-flex-chart-series&gt;
&lt;/wj-flex-chart&gt;</pre>
								<h3>The <b>control</b> property </h3>
								Each Wijmo control binding exposes a <b>control</b> property that references the Wijmo control instance created
								by the binding. This allows you to reference the control in view model code or in other bindings.
								<p>For example, the following markup creates a <a href="../classes/wijmo_grid.flexgrid.html">FlexGrid</a> control whose reference is stored in the <b>flex</b>
								observable property of a view model and is used in the button click event handler to move to the next grid record:</p>
								<pre>&lt;!-- HTML --&gt;
&lt;div data-bind="'wjFlexGrid': { itemsSource: data, control: flex }"&gt;&lt;/div&gt;
&lt;button data-bind="click: moveToNext"&gt;Next&lt;/button&gt;
&nbsp;
//View Model
this.flex = ko.observable();
this.moveToNext = function () {
    this.flex().collectionView.moveCurrentToNext();
}</pre>
								<h3>The <b>initialized</b> event</h3>
								Each Wijmo control binding exposes an <b>initialized</b> event and a Boolean <b>isInitialized</b>
								property. The event occurs right after the binding creates the control and fully initializes it
								with the values specified in the binding attributes. For bindings containing child bindings, for
								example, a <b>wjFlexGrid</b> with child <b>wjFlexGridColumn</b> bindings, this also means that
								child bindings have fully initialized and have been applied to the control represented by the
								parent binding. The isInitialized property is set to true right before triggering the initialized
								event. You can bind a view model observable property to the binding’s <b>isInitialized</b> property
								to access its value.
								<p>The following example adjusts FlexGridColumn formatting after the control fully initializes with its
								bindings, which guarantees that these formats are not overwritten with formats defined in the bindings:</p>
								<pre>&lt;!-- HTML --&gt;
&lt;div data-bind="'wjFlexGrid': { itemsSource: dataArray, initialized: flexInitialized }"&gt;
     &lt;div data-bind="wjFlexGridColumn: { binding: 'sales', format: 'n2' }"&gt;&lt;/div&gt;
     &lt;div data-bind="wjFlexGridColumn: { binding: 'downloads', format: 'n2' }"&gt;&lt;/div&gt;
&lt;/div&gt;
&nbsp;
//View Model
this.flexInitialized = function (data, sender, args) {
    var columns = sender.columns;
    for (var i = 0; i &lt; columns.length; i++) {
        if (columns[i].dataType = wijmo.DataType.Number) {
            columns[i].format = 'n0’;
        }
    }
}</pre>
								<h3 id="custom_elem_preproc">Custom elements preprocessor</h3>
								The Wijmo Knockout preprocessor uses the standard Knockout <a target="_blank" href="http://knockoutjs.com/documentation/binding-preprocessing.html">ko.bindingProvider.instance.preprocessNode</a>
								API. This may cause problems in cases where other custom preprocessors are used on the same page, because Knockout
								offers a single instance property for attaching a preprocessor function, and the next registering preprocessor
								removes the registration of the previous one.
								<p>To honor another attached preprocessor, the Wijmo Knockout preprocessor stores the currently registered preprocessor
									during initialization and delegates the work to it in cases where another processing node is not recognized
									as a Wijmo control element, thus organizing a preprocessor stack. But if you register another preprocessor
									after the Wijmo for Knockout preprocessor (that is, after the &lt;script&gt; reference to the <b>wijmo.knockout.js</b>
									module is executed) then you need to ensure that the other preprocessor behaves in a similar way;
								otherwise, the Wijmo Knockout preprocessor is disabled.</p>
								<p>If you prefer to disable the Wijmo Knockout preprocessor, set the <b>wijmo.disableKnockoutTags</b> property
									to false before the <b>wijmo.knockout.js</b> module reference and after the references to the core Wijmo
								modules, for example:</p>
								<pre>&lt;script src="scripts/wijmo.js"&gt;&lt;/script&gt;
&lt;script src="scripts/wijmo.input.js"&gt;&lt;/script&gt;
&lt;script&gt;
    wijmo.disableKnockoutTags = true;
&lt;/script&gt;
&lt;script src="scripts/wijmo.knockout.js"&gt;&lt;/script&gt;</pre>
								<p>Note that in this case you can use only the conventional data-bind syntax for adding Wijmo controls to the page
								markup; the Wijmo custom elements are not recognized.</p>
							</div>
						</section>
					</div>