Ich frage mich, ob ich auf die knockout.js HauptansichtModel von einer Methode außerhalb des Bereichs des ViewModel selbst zugreifen kann. Nehmen Sie dieses Beispiel:Zugriff auf ViewModel in JavaScript-Funktion außerhalb von ViewModel Bereich
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?
});
}
Vielen Dank für diese (richtige) Antwort, aber ich frage mich, ob es noch ist eine Möglichkeit, die Informationen aus dem Kontext heraus zu beziehen, nur für zukünftige Referenz? –
hinzugefügt diese Details im Vordergrund, ko.dataFor und ko.contextFor nur brauchen den Dom-Knoten, den Sie mit Blick auf das Ereignis in Ihrer Handler-Funktion bekommen können. $ root für das Ergebnis von contextFor wäre Ihr View-Modell –