2013-12-10 62 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