为什么这个事件在淘汰赛中被处理两次?

时间:2014-03-18 19:33:43

标签: knockout.js custom-binding

出于某种原因,以下代码生成链接'点击事件火两次。我淘汰比较新,我想我可能做错了自定义绑定。谁能告诉我我做错了什么? (顺便说一下,我没有发布小提琴的原因是我不能在jsfiddle上包含来自github的映射插件。)

JS:

ko.bindingHandlers.activityContent = {
    init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        // This will be called when the binding is first applied to an element
        // Set up any initial state, event handlers, etc. here
        var content = document.createElement("p");
        content.innerHTML = '<a href="javascript:void(0)" data-bind="text: user_name, click: $parent.NavigatePage.bind($data, \'profile\', user_id)"></a>';
        element.appendChild(content);
        ko.applyBindings(bindingContext, content);
    },
    update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        // This will be called once when the binding is first applied to an element,
        // and again whenever the associated observable changes value.
        // Update the DOM element based on the supplied values here.
    }
};

var activities = ko.mapping.fromJS({Activities: [{
    "user_id": "52b5042d572b94ceadf6asdf1a2a5bc",
    "user_name": "Sean Templeton"
}, {
    "user_id": "52b5042d57asfda2b94ce61a2a5bc",
    "user_name": "Sean Templeton"
}, {
    "user_id": "52b5042d572b94ce61a2a5bc",
    "user_name": "Sean Templeton"
}, {
    "user_id": "52b5042d5asdfasdf72b94ce61a2a5bc",
    "user_name": "Sean Templeton"
}, {
    "user_id": "52basdf5042d572b94ce6asdf1a2a5bc",
    "user_name": "Sean Templeton"
}], NavigatePage: function(page, userId) { console.log(this); console.log(page); console.log(userId()); }});

ko.applyBindings(activities);

HTML:

<Ul data-bind="foreach: Activities">
    <li data-bind="activityContent: $data"></li>
</ul>

1 个答案:

答案 0 :(得分:0)

删除ko.applyBindings(bindingContext, content);中的init行,并记住update在init上运行一次,然后在每次更新时运行。

另外,你有一个更大的问题。在@nemesv上面给出的小提琴中,通过不从自定义绑定中删除ko.applyBindings(),列表甚至不会完整地重复5次。

一种不同的方法

除非您对此示例之外的activityContent绑定有很好的计划,否则您可以省去自定义绑定,只需在viewModel中的<li>元素上注册委托事件即可。然后,您可以使用Knockout ko.dataFor()来确定点击了什么,然后调用NavigatePage()。这种方法效率更高,不需要自定义绑定。

您可以阅读有关委托事件here的信息。