How to apply binding and persist input value using Knockout JS?


Kees C. Bakker

I am building an HTML/KnockoutJS application. My web server returns a form with input fields and information. When I update the model and execute ko.applyBindings, the input value is naturally overwritten by the model.

Is there a way to automatically load the input field's data ko.applyBindingsinto the model ?


Example : https://jsfiddle.net/KeesCBakker/p7ygq5y2/1/ _

HTML:

Title: <input data-bind="textInput: title" value="MyTitle" placeholder="Nothing here!" /><br/>
Text: <input data-bind="textInput: text" value="MyText" placeholder="Nothing here!" /><br/>
<button id="bind">Bind!</button>

JS:

ko.bindingHandlers.initFromInput = {
  init: function(element, valueAccessor) {
    valueAccessor()(element.value);
  }
};

function Model() {
  this.title = ko.observable();
  this.text = ko.observable();
}

document.getElementById('bind').onclick = function() {

  var model = new Model();
  ko.applyBindings(model);

};
clean_coding

You can use a custom binding to tell Knockout to use the input value as the default, like this:

ko.bindingHandlers.initFromInput = {
    init: function(element, valueAccessor) {
        valueAccessor()(element.value);
    }
};

Here is a jsfiddle : http://jsfiddle.net/kv3zras3/3/

edit:

With the new binding, your data binding should look like this:

<input data-bind="initFromInput: title, value: title" value="MyTitle" placeholder="Nothing here!" />
<input data-bind="initFromInput: text, value:text" value="MyText" placeholder="Nothing here!" />

edit:

There is a better way to do it if you make the bindings look like this:

var origValueInput = ko.bindingHandlers.value.init;
ko.bindingHandlers.value.init = function(element, valueAccessor, allBindings) {
    if (allBindings.has('initValueFromInput')) {
        valueAccessor()(element.value);
    }
    origValueInput.apply(this, arguments);
};

You can write data binding like this:

<input value="MyTitle" data-bind="initValueFromInput, value: title"/>
<input value="MyText" data-bind="initValueFromInput, value: text"/>

Here is a fiddle : https://jsfiddle.net/yy51kok5/

Related


How to apply binding and persist input value using Knockout JS?

Kees C. Bakker I am building an HTML/KnockoutJS application. My web server returns a form with input fields and information. When I update the model and execute ko.applyBindings, the input value is naturally overwritten by the model. Is there a way to automati

How to apply binding and persist input value using Knockout JS?

Kees C. Bakker I am building an HTML/KnockoutJS application. My web server returns a form with input fields and information. When I update the model and execute ko.applyBindings, the input value is naturally overwritten by the model. Is there a way to automati

How to apply binding and persist input value using Knockout JS?

Kees C. Bakker I am building an HTML/KnockoutJS application. My web server returns a form with input fields and information. When I update the model and execute ko.applyBindings, the input value is naturally overwritten by the model. Is there a way to automati

How to apply binding and persist input value using Knockout JS?

Kees C. Bakker I am building an HTML/KnockoutJS application. My web server returns a form with input fields and information. When I update the model and execute ko.applyBindings, the input value is naturally overwritten by the model. Is there a way to automati

Knockout JS binding doesn't output value using simple viewmodel

Carl Weiss I'm trying to load a simple view model that has two models: Profile (a ko.observable()) and Request (a ko.observableArray()). When I apply the binding and then try to bind to the ether profile.field or foreach: request, I get no output, no javascrip

Knockout.js - "value" binding in "html" binding

thread lemon I'm working on an application that needs to generate HTML dynamically based on some values. I put the following code where the dynamic HTML is: <div data-bind="html: extraHTML"></div> I have an object setup in my javascript file that contains var

Knockout.js - "value" binding in "html" binding

thread lemon I'm working on an application that needs to generate HTML dynamically based on some values. I put the following code where the dynamic HTML is: <div data-bind="html: extraHTML"></div> I have an object setup in my javascript file that contains var

How to apply Knockout binding to <html> element?

Connected I want to dynamically set the font size of the root element using a custom Knockout binding handler that calculates based on the width of the browser window. When I try to apply the binding, nothing seems to happen, so I try to apply a simple css bin

About data binding by knockout js and value change

Thomas There are two textboxes and a span bound by culling. The example I got is easy to understand. Here is the code. <p>First name: <input data-bind="value: firstName" /></p> <p>Last name: <input data-bind="value: lastName" /></p> <h2>Hello, <span data-bind=

About data binding by knockout js and value change

Thomas There are two textboxes and a span bound by culling. The example I got is easy to understand. Here is the code. <p>First name: <input data-bind="value: firstName" /></p> <p>Last name: <input data-bind="value: lastName" /></p> <h2>Hello, <span data-bind=

Knockout Js radio binding not setting value

Callum Linington Here's the documentation I'm looking at: Example of adding a radio button it says: And only if the parameter value is equal to the value attribute of the radio button node, KO will set the element to check What I did here: jsfiddle self.radioV

About data binding by knockout js and value change

Thomas There are two textboxes and a span bound by culling. The example I got is easy to understand. Here is the code. <p>First name: <input data-bind="value: firstName" /></p> <p>Last name: <input data-bind="value: lastName" /></p> <h2>Hello, <span data-bind=

Knockout Js radio binding not setting value

Callum Linington Here's the documentation I'm looking at: Example of adding a radio button it says: And only if the parameter value is equal to the value attribute of the radio button node, KO will set the element to check What I did here: jsfiddle self.radioV

Knockout binding using key-value pairs

Ashishraaj I'm doing knockout with BreezeJs to retrieve and store data from a local database. The problem I am facing here is the key value binding in the menu. What I do is display and select the "Name" property from the select menu and store the correspondin

Knockout binding using key-value pairs

Ashishraaj I'm doing knockout with BreezeJs to retrieve and store data from a local database. The problem I am facing here is the key value binding in the menu. What I do is display and select the "Name" property from the select menu and store the correspondin

Knockout.js data binding using jQuery

username I have a button that I want to bind to a method in the VM using knockout. I am using the following code <button type="button" class="btn btn-primary" id="cmdCreateConnection" data-bind="click: function(data, event) {

Knockout.js data binding using jQuery

username I have a button that I want to bind to a method in the VM using knockout. I am using the following code <button type="button" class="btn btn-primary" id="cmdCreateConnection" data-bind="click: function(data, event) {