KnockoutJS: Event object

余生长醉 提交于 2019-12-11 16:45:11

问题


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>

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.


回答1:


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.



来源:https://stackoverflow.com/questions/5292197/knockoutjs-event-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!