2013-12-10 102 views
5

我是一個相對較新的使用淘汰賽JavaScript庫。我有一個問題,得到一個可觀察的屬性,這是另一個對象的對象。這裏是我的代碼:JavaScript淘汰賽的綁定嵌套對象不工作

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

我該如何去約束客戶對象中的屬性。我嘗試用典型的JavaScript點符號,像這樣:customer.customer_id

這裏是結合數據的HTML:

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

回答

11

由於customer是可觀察到的,你必須UNROLL它在你的綁定。因此,這將是這樣的:

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

同樣,這

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

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

@ user2864740 - 嗯,這個http://knockoutjs.com/documentation/custom-bindings-c ontrolling-descendant-bindings.html看起來很酷。添加到我的閱讀列表:) –

+0

亞當,感謝您的迴應。信不信由你,這部分工作:alert(「New id:」+ self.customer.customer_id()+「num:」+ c.custnum);但是,相同的引用不能在我的html綁定中工作。 – user3088317

-2

試試這個,應該是有幫助的:

<div class="field-input" style="margin-bottom:10px;"> 
    <input type="text" id="customer_id" style="width:100%;" data-bind="value: customer().customer_id, disabled: true" /> 
</div> 
+0

歡迎來到SO,但請停止添加與現有複本重複的答案。 – JohnnyHK