我对使用淘汰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) {
}
}
如何绑定到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>
答案 0 :(得分:12)
由于customer
是一个可观察的,您必须在绑定中展开。所以它会是这样的:
<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
答案 1 :(得分:-3)
试试这个,应该会有所帮助:
<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>