我想知道是否可以从 viewModel 本身范围之外的方法访问 knockout.js 主 viewModel。举个例子:
function Employee(data) {
var self = this;
ko.mapping.fromJS(data, {}, this);
}
function EmployeeViewModel() {
var self = this;
this.employees = ko.observableArray([]);
this.loadEmployees = function() {
var mappedEmployees= $.map(JSON.parse(data.value), function(item) { return new Employee(item) });
self.employees (mappedEmployees);
}
}
// here's the part I'm curious about
$(document).ready(function() {
ko.applyBindings(new EmployeeViewModel());
$("#myLink").click(function() {
// is there some way to get back into the ko context here?
// like with ko.dataFor or ko.contextFor?
});
}