Knockout.js: Is it possible to use "with" for one-way data binding?


crazy synth

In the Knockout documentation it is said that one-way data binding is possible if we don't use ko.observable(...) : http://knockoutjs.com/documentation/value-binding.html

However, the following code doesn't work as I expect:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>

<body>
    <form data-bind="with: selectedMerchant">
        <input data-bind="value: name"></input>
        <button data-bind="click: change"> CHANGE </button>
        <button data-bind="click: show"> SHOW </button>
    </form>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-debug.js" type="text/javascript"></script>
    <script>
        function PartnersViewModel() {
            self.selectedMerchant = {'name': 'John'};
            self.show = function() {
                alert(JSON.stringify(self.selectedMerchant));
            }
            self.change = function() {
                self.selectedMerchant = {'name': 'David'};
            }
        }
        ko.applyBindings(new PartnersViewModel());
    </script>
</body>
</html>

'selectedMerchant' is declared without ko.observable(...), so it should be bound one way. I want the text in the input box to change to "David" once I click the "change" button. I also tried different orientations - I changed the text in the input and clicked the "Show" button. The message box gives its initial value at the beginning.

Jason speaks

If you want a true one-way binding to continuously update the UI, you can use computed observables. Unless you specify a "write" property for the calculation, it will only update in the Model->UI direction. Having said that, you can achieve the same result by simply replacing the input box with a span or a label to prevent the user from updating the value, so I'm not sure I see the utility of one-way binding.

function PartnersViewModel() {
    var self = this;
    self.selectedMerchant = ko.observable({'name': 'John'});
    self.show = function() {
        alert(JSON.stringify(self.selectedMerchant()));
    }
    self.change = function() {
        self.selectedMerchant({'name': 'David'});
    }
    self.selectedName = ko.computed(function(){
      return self.selectedMerchant().name;
    });
}
ko.applyBindings(new PartnersViewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-debug.js" type="text/javascript"></script>
    
<div data-bind="with: selectedMerchant">
  <input data-bind="value: $parent.selectedName"></input>
  <button data-bind="click: $parent.change"> CHANGE </button>
  <button data-bind="click: $parent.show"> SHOW </button>
</div>

If you want something more reusable, you can make a custom binding that implements only one direction:

ko.bindingHandlers.oneWayValue = {
    update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        var value = ko.unwrap(valueAccessor());
        element.value = value;
    }
}

Related


How to use one-way binding in knockout

Nixon I have a viewModel like this: var viewModel = { firstName: 'Fred' } and bind a textbox to it like this <input data-bind="value: firstName" > I was under the impression that this would establish a one-way binding since the firstName

How to use one-way binding in knockout

Nixon I have a viewModel like this: var viewModel = { firstName: 'Fred' } and bind a textbox to it like this <input data-bind="value: firstName" > I was under the impression that this would establish a one-way binding since the firstName

How to use one-way binding in knockout

Nixon I have a viewModel like this: var viewModel = { firstName: 'Fred' } and bind a textbox to it like this <input data-bind="value: firstName" > I was under the impression that this would establish a one-way binding since the firstName

How to use one-way binding in knockout

Nixon I have a viewModel like this: var viewModel = { firstName: 'Fred' } and bind a textbox to it like this <input data-bind="value: firstName" > I was under the impression that this would establish a one-way binding since the firstName

Ajax data binding with Knockout Js

Kitty Sarvaj I am using knockout js and I am finding it difficult to bind data in ajax get method, I have created model, viewModel and ajax function, I have ajax method in the same js file where I am creating viewModel on page load ajax and try to bind my html

Ajax data binding with Knockout Js

Kitty Sarvaj I am using knockout js and I am finding it difficult to bind data in ajax get method, I have created model, viewModel and ajax function, I have ajax method in the same js file where I am creating viewModel on page load ajax and try to bind my html

Knockout js: data binding click not working

Benjamin Baggins I can't trigger the knockout feature logMyStuffon my website with a simple button click . Note that I've added <button>, <a>and <div>tried to get it to work. I made a JS fiddle, but the JS fiddle worked. Here is my code: var SearchFilterViewMo

Knockout.js data binding to ViewModel

Damon I'm trying to figure out KnockOut data binding and am struggling to get a simple form to bind to a ViewModel. I am using WebAPI to extract my JSON data. This is my ViewModel, when this "find" method is called, it creates a new WorkOrder object and popula

Knockout.js data binding to ViewModel

Damon I'm trying to figure out KnockOut data binding and am struggling to get a simple form to bind to a ViewModel. I am using WebAPI to extract my JSON data. This is my ViewModel, when this "find" method is called, it creates a new WorkOrder object and popula

Does data binding show div knockout js

XAF So I'm trying to display some divs that basically show boxes of different colors based on their number, basically both AA and B return 5, so they should show, while other divs should be empty or empty, or even not show the divs at all, But for some reason

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=

styled data binding not working in knockout js

Govan violin Style bindings don't work in knockout js. I don't know why it doesn't work? Code: <div class="sample" data-bind="style:{color:'blue'}"> hai </div> please help me. Damian You forgot to call ko.applyBindings(). see violin

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 - Binding select list to data object

Charlie Pope objective I wish to create a claim form. The claim form must support the following: Add (create) claim line Store (read) all claim lines Edit (update) claim line Delete (destroy) claim lines Display a variable number of fields based on user select

Knockout.js data binding to ViewModel

Damon I'm trying to figure out KnockOut data binding and am struggling to get a simple form to bind to a ViewModel. I am using WebAPI to extract my JSON data. This is my ViewModel, when this "find" method is called, it creates a new WorkOrder object and popula

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=

Does data binding show div knockout js

XAF So I'm trying to display some divs that basically show boxes of different colors based on their number, basically both AA and B return 5, so they should show, while other divs should be empty or empty, or even not show the divs at all, But for some reason

Knockout JS - Binding select list to data object

Charlie Pope objective I wish to create a claim form. The claim form must support the following: Add (create) claim line Store (read) all claim lines Edit (update) claim line Delete (destroy) claim lines Display a variable number of fields based on user select

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) {

From two way data binding to one way data binding angularjs

Abdul Hamid | I have this directive <directive dataset="dataObject"></directive> My directive link function has something like this: var oldData = ''; var initFunction = function(){ oldData = scope.dataObject; } scope.reset = function(){ scope.dataObjec

From two way data binding to one way data binding angularjs

Abdul Hamid | I have this directive <directive dataset="dataObject"></directive> My directive link function has something like this: var oldData = ''; var initFunction = function(){ oldData = scope.dataObject; } scope.reset = function(){ scope.dataObjec

From two way data binding to one way data binding angularjs

Abdul Hamid | I have this directive <directive dataset="dataObject"></directive> My directive link function has something like this: var oldData = ''; var initFunction = function(){ oldData = scope.dataObject; } scope.reset = function(){ scope.dataObjec

From two way data binding to one way data binding angularjs

Abdul Hamid | I have this directive <directive dataset="dataObject"></directive> My directive link function has something like this: var oldData = ''; var initFunction = function(){ oldData = scope.dataObject; } scope.reset = function(){ scope.dataObjec