KnockoutJS:事件对象 [英] KnockoutJS: Event object
问题描述
此问题与 KnockoutJS:跟踪菜单的点击次数有关.我已经能够通过提供一个id值来判断单击了哪个菜单.我需要更改此模型,并使用event.target来获取其他信息.我尝试了此,但它似乎不起作用.我还创建了全局函数menuClicked
:
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: 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>
但是,每次我单击超链接时,事件对象都是null/undefined.我正在尝试做的是检索用于呈现超链接的对象,如此示例除了我的超链接没有ID.
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.
推荐答案
事件对象被传递到click绑定,但仅以最新的Knockout代码(因此在1.12版本之后)传递.它将是1.2版本,应该在不久之后发布.
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.
您可以在此处获取最新代码: https://github.com/SteveSanderson/淘汰赛/tree/master/build/output
You can get the latest code here: https://github.com/SteveSanderson/knockout/tree/master/build/output
它非常稳定.希望这会有所帮助.
It is quite stable. Hope this helps.
这篇关于KnockoutJS:事件对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!