You can always access it just by storing your viewmodel in a variable you can access, the module and revealing module patterns are nice, but you can just store it in an object that won't conflict with other names ('my' here):
my = { viewModel: new EmployeeViewModel() };
ko.applyBindings(my.viewModel);
You had the right idea, you just need to take the element as an argument to your click function and pass the target property to ko.dataFor or ko.contextFor (jsfiddle):
$(document).ready(function() {
my.vm = new EmployeeViewModel();
ko.applyBindings(my.vm);
$('button').on('click', function(e) {
alert('you clicked on employee ' + ko.contextFor(e.target).$data.name() +
' also known as ' + ko.dataFor(e.target).name());
});
});?
Instead of using jQuery to bind the click event, you can always use the Knockout click binding, which passes the current model data as the first parameter and the event as the second parameter (jsfiddle):
function EmployeeViewModel() {
var self = this;
// skipped some of your code
this.clickedOnEmployee = function(model, event) {
// assuming your Employee has a name observable
alert('You clicked on employee ' + model.name());
};
}
In your html (the $root is your base view model, you wouldn't need it if your clickedOnEmployee function was on your Employee objects):
<ul data-bind="foreach: employees">
<li>
<span data-bind="text: name"></span>
<button data-bind="click: $root.clickedOnEmployee">show</button>
</li>
</ul>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…