knockoutjs 重写绑定处理程序

发布于 2024-12-28 15:47:26 字数 81 浏览 0 评论 0原文

您好,我正在尝试设置 ko,以便在任何被调用的单击处理程序上运行一点自定义代码。 将一些前置和后置代码添加到“单击”绑定处理程序的最简单方法是什么?

Hi I'm trying to set ko up so that on any click handler being called a little bit of custom code is run.
Whats the easiest way to add some pre and post code to the 'click' bindings handler?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请你别敷衍 2025-01-04 15:47:26

您可以创建一个包装 click 绑定的自定义绑定,也可以保存对 click 的原始 initupdate 函数的引用 绑定并替换真实的。

您可以选择在 update 函数中执行一些代码,这些代码将在模型值更新时触发(通过 init 函数中附加的事件处理程序或以编程方式),或者将您的代码作为实际的处理程序。在我看来你想要后者。

您的绑定可能如下所示:

(function() {
    var originalInit = ko.bindingHandlers.click.init,
        originalUpdate = ko.bindingHandlers.click.update;

    ko.bindingHandlers.click = {
        init: function(element, valueAccessor, allBindingsAccessor, viewModel, context) {
            var wrappedValueAccessor = function() {
                return function(data, event) {
                   //run some pre code
                   ko.bindingHandlers.click.preOnClick.call(viewModel, data, event);

                   valueAccessor().call(viewModel, data, event);

                   //run some post code
                   ko.bindingHandlers.click.postOnClick.call(viewModel, data, event);
                };

            };

            originalInit(element, wrappedValueAccessor, allBindingsAccessor, viewModel, context);
        },
        update: originalUpdate,
        preOnClick: function(data, event) {
            alert("pre code for " + data.id);
        },
        postOnClick: function(data, event) {
            alert("post code for " + data.id);
        }
    };
})();

我拆分了前/后代码,以便在运行时您可以覆盖 ko.bindingHandlers.click.preOnClickko.bindingHandlers.click.postOnClick代码>

这是一个示例: http://jsfiddle.net/rniemeyer/PksAn/

如果您需要在更新函数中运行自定义代码,那么您可以将其拆分出来并在那里运行您的前后代码并执行 之间的originalUpdate

You can either create a custom binding that wraps the click binding or save off references to the original init and update functions of the click binding and replace the real one.

You could either choose to execute some code in the update function which will be triggered when the model value is updated (either by the event handler attached in the init function or programmatically) or execute your code as part of the actual handler. It sounds to me like you want the latter.

Your binding might look like:

(function() {
    var originalInit = ko.bindingHandlers.click.init,
        originalUpdate = ko.bindingHandlers.click.update;

    ko.bindingHandlers.click = {
        init: function(element, valueAccessor, allBindingsAccessor, viewModel, context) {
            var wrappedValueAccessor = function() {
                return function(data, event) {
                   //run some pre code
                   ko.bindingHandlers.click.preOnClick.call(viewModel, data, event);

                   valueAccessor().call(viewModel, data, event);

                   //run some post code
                   ko.bindingHandlers.click.postOnClick.call(viewModel, data, event);
                };

            };

            originalInit(element, wrappedValueAccessor, allBindingsAccessor, viewModel, context);
        },
        update: originalUpdate,
        preOnClick: function(data, event) {
            alert("pre code for " + data.id);
        },
        postOnClick: function(data, event) {
            alert("post code for " + data.id);
        }
    };
})();

I split out the pre/post code such that at run-time you could override ko.bindingHandlers.click.preOnClick or ko.bindingHandlers.click.postOnClick

Here is a sample: http://jsfiddle.net/rniemeyer/PksAn/

If you need to run custom code in the update function, then you can split it out and run your pre and post code there and execute originalUpdate in between.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文