Greifen Sie auf viewModel in der JavaScript-Funktion außerhalb des Bereichs von viewModel zu
Ich frage mich, ob ich von einer Methode außerhalb des Bereichs von viewModel selbst auf knockout.js main viewModel zugreifen kann. Nehmen Sie dieses Beispiel:
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?
});
}