admin管理员组文章数量:1391987
I'm writing an application for making a reservation. The reservation procedure is rather plex and has quite some dependencies so I decided to use knockout to help me observe changes and update the UI.
I started implementing the customer list. The first customer in the form will be the one who has to enter his details, the others only need names. I figured I could just add a dependentObservable that checks if the current customer is the first one in the customers array to decide whether to display the additional fields.
The problem is that when trying to access viewModel from a customer, I get only get 'undefined'. I tried passing a reference to viewModel to the customer, but that didn't work either. What am I doing wrong? Can viewModel not be accessed?
Here's the code:
var customer = function(){
this.firstName = ko.observable('');
this.lastName = ko.observable('');
this.fullName = ko.dependentObservable(
function(){
return this.firstName() + " " + this.lastName();
},
this
);
this.gender = ko.observable('');
this.diet = ko.observable('');
this.primaryCustomer = ko.dependentObservable(
function(){
console.log(viewModel);
return viewModel.customers.indexOf(this) == 0;
},
this
);
this.email = ko.observable('');
}
var viewModel = {
customers: ko.observableArray([new customer()]),
addCustomer: function(){
this.customers.push(new customer());
},
removeCustomer: function(customer){
this.customers.remove(customer);
}
}
ko.applyBindings(viewModel);
I'm writing an application for making a reservation. The reservation procedure is rather plex and has quite some dependencies so I decided to use knockout to help me observe changes and update the UI.
I started implementing the customer list. The first customer in the form will be the one who has to enter his details, the others only need names. I figured I could just add a dependentObservable that checks if the current customer is the first one in the customers array to decide whether to display the additional fields.
The problem is that when trying to access viewModel from a customer, I get only get 'undefined'. I tried passing a reference to viewModel to the customer, but that didn't work either. What am I doing wrong? Can viewModel not be accessed?
Here's the code:
var customer = function(){
this.firstName = ko.observable('');
this.lastName = ko.observable('');
this.fullName = ko.dependentObservable(
function(){
return this.firstName() + " " + this.lastName();
},
this
);
this.gender = ko.observable('');
this.diet = ko.observable('');
this.primaryCustomer = ko.dependentObservable(
function(){
console.log(viewModel);
return viewModel.customers.indexOf(this) == 0;
},
this
);
this.email = ko.observable('');
}
var viewModel = {
customers: ko.observableArray([new customer()]),
addCustomer: function(){
this.customers.push(new customer());
},
removeCustomer: function(customer){
this.customers.remove(customer);
}
}
ko.applyBindings(viewModel);
Share
Improve this question
edited Feb 19, 2012 at 15:33
Tim
asked Jul 13, 2011 at 20:56
TimTim
2,45019 silver badges20 bronze badges
0
1 Answer
Reset to default 5I figured it out. The idea of passing the viewModel to the customer was the right one, just the execution was bad. When I initialized customers I did it with a new customer, which in turn looked for customers which was not there yet.
Here's the working code:
var customer = function(viewModel){
this.firstName = ko.observable('');
this.lastName = ko.observable('');
this.fullName = ko.dependentObservable(
function(){
return this.firstName() + " " + this.lastName();
},
this
);
this.gender = ko.observable('');
this.diet = ko.observable('');
this.primaryCustomer = ko.dependentObservable(
function(){
console.log(viewModel);
return viewModel.customers.indexOf(this) == 0;
},
this
);
this.email = ko.observable('');
}
var viewModel = {
customers: ko.observableArray(),
removeCustomer: function(customer){
this.customers.remove(customer);
}
}
viewModel.customers.push(new customer(viewModel));
viewModel.addCustomer = function(){
viewModel.customers.push(new customer(viewModel));
}
ko.applyBindings(viewModel);
本文标签: javascriptKnockout JSviewModel accessStack Overflow
版权声明:本文标题:javascript - Knockout JS - viewModel access - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744723848a2621855.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论