Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript knockout binding nested objects not working

I'm am relatively new to the use of the knockout javascript library. I'm having a problem getting an observable property which is an object of another object. Here is my code:

function Customer(id) {
    var self = this;

    self.customer_id = ko.observable(id);
    self.custnum = -1;

    self.busname = ko.observable("");
    self.address = "";
    self.city = "";
    self.state_id = "";
    self.zipcode = "";
    self.cnt_sal_id = "";
    self.cnt_first_name = "";
    self.cnt_last_name = "";
    self.cnt_title = "";

    //alert("customer: " + self.customer_id());

}


var CustomerEntryViewModel = function(date) {
    var self = this;

    self.last_update = ko.observable(date);
    self.customer = ko.observable(new Customer(""));

    self.addCustomer = function (id) {
        var c = new Customer(id);
        self.customer = c;
        alert("New id: " + self.customer.customer_id() + " num: " + c.custnum);
    }

    self.customerSearch = function () {
    }

    self.editCustomer = function (customer_id) {
    }

    self.save = function(customer) {    
    }           
}

How do I go about binding to the properties in the Customer object. I try to to use typical javascript dot notation like so: customer.customer_id

Here is the html that binds the data:

<div class="field-input" style="margin-bottom:10px;">
    <input type="text" id="customer_id" style="width:100%;" 
        data-bind="jqxInput: { placeHolder: 'Customer #', value: 
                               customer().customer_id, height: 21, width: 208, 
                               minLength: 1, disabled: true }"/>
</div>
like image 679
user3088317 Avatar asked Dec 10 '13 19:12

user3088317


People also ask

How do we activate a Knockout model?

To activate Knockout, add the following line to a <script> block: ko. applyBindings(myViewModel); You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery's $ function.

What are the types of binding supported by Knockout JS?

Binding Values The binding value can be a single value, literal, a variable or can be a JavaScript expression.

What is two way binding in Knockout JS?

KO is able to create a two-way binding if you use value to link a form element to an Observable property, so that the changes between them are exchanged among them. If you refer a simple property on ViewModel, KO will set the form element's initial state to property value.

Is Knockout js a framework or library?

Knockout. js is an open source library (under the MIT License) that is pure JavaScript that works with any existing web framework and every mainstream browser. Further, Knockout. js is fully documented with a complete set of online tutorials.


1 Answers

Since customer is an observable, you have to unroll it in your bindings. So it would be something like:

<div data-bind="text: customer().address"></div>

And similarly, this

alert("New id: " + self.customer.customer_id() + " num: " + c.custnum);

would be

alert("New id: " + self.customer().customer_id() + " num: " + c.custnum);
                          //     ^ unrolled
like image 75
Adam Rackis Avatar answered Nov 09 '22 05:11

Adam Rackis