Javascript knockout绑定的嵌套对象不工作 [英] Javascript knockout binding nested objects not working
本文介绍了Javascript knockout绑定的嵌套对象不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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(client:+ 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){
}
}
如何绑定到Customer对象中的属性。我尝试使用像这样的典型的JavaScript点符号:customer.customer_id
这是绑定数据的html:
< div class =field-inputstyle =margin-bottom:10px;>
< input type =textid =customer_idstyle =width:100%;
data-bind =jqxInput:{placeHolder:'Customer#',value:
customer()。customer_id,height:21,width:208,
minLength:1,disabled:true }/>
< / div>
解决方案
由于客户
是一个可观察的,你必须在你的绑定中展开。所以这将是这样的:
< div data-bind =text:customer()。address>< ; / div>
同样,这个
alert(New id:+ self.customer.customer_id()+num:+ c.custnum);
将是
code> alert(New id:+ self.customer()。customer_id()+num:+ c.custnum);
// ^展开
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>
解决方案
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
这篇关于Javascript knockout绑定的嵌套对象不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文