admin管理员组文章数量:1287898
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 = "";
selft_sal_id = "";
selft_first_name = "";
selft_last_name = "";
selft_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>
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>
Share
Improve this question
edited Dec 10, 2013 at 20:01
nemesv
140k16 gold badges423 silver badges362 bronze badges
asked Dec 10, 2013 at 19:46
user3088317user3088317
611 silver badge2 bronze badges
2 Answers
Reset to default 12Since 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
Try this, should be helpful :
<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>
本文标签: htmlJavascript knockout binding nested objects not workingStack Overflow
版权声明:本文标题:html - Javascript knockout binding nested objects not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741330772a2372753.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论