This question is related to KnockoutJS: Tracking menu clicks. I have been able to tell which menu was clicked by supplying an id value. I need to change this model and use the event.target to obtain additional information. I tried this but it does not seem to work. I also created global function menuClicked
:
var viewModel = {};
function menuClicked(event) {
var id = ($(event.target).tmplItem().data).Id;
var isActive = viewModel.menuActive();
if (!isActive || viewModel.currentMenu() == id)
viewModel.menuActive(!isActive);
viewModel.currentMenu(id);
}
$(function () {
$.ajax({
url: 'console.asmx/Initialize',
type: "POST",
cache: false,
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: 开发者_JAVA技巧function (data) {
viewModel = data.d;
viewModel.menuActive = ko.observable(false);
viewModel.currentMenu = ko.observable(0);
ko.applyBindings(viewModel);
}
});
});
And bound the hyperlinks to that method:
<a class='${ Class }' data-bind='click: menuClicked'>${ Name }</a>
But each time I click the hyperlink, the event object is null/undefined. What I'm trying to do is to retrieve the object used to render the hyperlink as in this example except my hyperlinks do not have ids.
Any assistance is greatly appreciated.
The event object is passed to the click binding, but only in the latest Knockout code (so after the 1.12 release). It will be in the 1.2 release, which should be out before too long.
You can get the latest code here: https://github.com/SteveSanderson/knockout/tree/master/build/output
It is quite stable. Hope this helps.
精彩评论