任何 viewmodel dom 更新的 Knockout JS 事件

发布于 2025-01-03 15:46:14 字数 230 浏览 2 评论 0原文

每当通过 Knockout.JS 更新 DOM 时,我都需要运行一段 jquery ui 代码。我意识到这可以使用自定义绑定来完成,但这似乎与特定的视图模型相关,我想在全局范围内执行此操作,因此每当它发生在它触发的任何视图模型上时?

可以说,我总是希望在所有具有“needsdate”类的文本框中使用 JQuery 日期选择器,而不是将其添加到我的每个视图模型中,如果我可以全局执行此操作,那就太好了。

这可能吗?

I need to run a jquery ui bit of code whenever the DOM is updated via Knockout.JS. I realize this can be done using custom bindings, but that appears to be related to a specific viewmodel, I want to do it globally so whenever it happens on any viewmodel it fires?

Lets say I ALWAYS want a JQuery datepicker on all textboxes with class 'needsdate', rather than add this to each of my view models, it would be great if I could do it globally.

Is this possible?

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

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

发布评论

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

评论(2

兔姬 2025-01-10 15:46:14

如果您不打算动态添加/删除元素,那么您可以像平常一样连接它们。但是,如果您正在处理动态内容(例如使用修改其项目的 observableArray),那么您有几个选择:

1- 就像答案 此处,您可以创建自定义绑定。如果您不想将值绑定到视图模型上的属性,那么您可以将绑定简化为:

ko.bindingHandlers.datepicker = {
    init: function(element, valueAccessor) {
        //initialize datepicker with some optional options
        var options = ko.utils.unwrapObservable(valueAccessor());
        $(element).datepicker(options);

        //handle disposal (if KO removes by the template binding)
        ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
            $(element).datepicker("destroy");
        });
    }
}; 

您可以将其放置在如下元素上:

2-另一个选项是使用 afterRender template 的功能(以及 foreach< /code> 使用 template) 在呈现新内容后连接日期选择器。

If you are not going to be dynamically adding/removing elements, then you could just wire them up as normal. However, if you are working with dynamic content (like using an observableArray that is having its items modified), then you have a couple of options:

1- Like the answer here, you can create a custom binding. If you don't want to bind the value to a property on your view model, then you can simplify the binding to something like:

ko.bindingHandlers.datepicker = {
    init: function(element, valueAccessor) {
        //initialize datepicker with some optional options
        var options = ko.utils.unwrapObservable(valueAccessor());
        $(element).datepicker(options);

        //handle disposal (if KO removes by the template binding)
        ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
            $(element).datepicker("destroy");
        });
    }
}; 

You would place it on an element like:

<input data-bind="datepicker: { minDate: new Date() }" />

2- the other option is to use the afterRender functionality of the template (and foreach which uses template) to wire up your datepickers after new content is rendered.

つ可否回来 2025-01-10 15:46:14

我创建了一个 refreshJQuery() 函数,可以一次性应用我所有的强大行为。然后,当我订阅视图模型中的可观察对象以更新模型时,我会调用此函数。一旦页面加载。

function refreshJQuery() {
    $(".draggable, .droppable, .resizable").each(function () {

        var $element = $(this);

        if ($element.hasClass("draggable") && !$element.data("init-draggable")) {
            $element.data("init-draggable", true).draggable(dragBehavior);
        }
        if ($element.hasClass("droppable") && !$element.data("init-droppable")) {
            $element.data("init-droppable", true).droppable(dropBehavior);
        }
        if ($element.hasClass("resizable") && !$element.data("init-resizable")) {
            $element.data("init-resizable", true).resizable(resizeBehavior);
            $(this).children().fadeTo(2000, 0);
            $element.hover(function () {
                $(this).children().fadeTo(200, 1);
            },
            function () {
                $(this).children().fadeTo(750, 0);
            });
        }
    });
};

然后我按如下方式使用它,尽管我已经确定了另外 2 个可观察量,但我还需要从中调用此函数:

this.updateTime = function () {
    service.updateBookingTimes(this.id(), this.startDate(), this.endDate());
    refreshJQuery();
};

this.startDate.subscribe(this.updateTime, this);
this.endDate.subscribe(this.updateTime, this);

我还实现了 @RPNiemeyer 的字段级 JQuery 订阅解决方案,因此除了他的日期选择器示例之外,我还实现了类似的代码将 $.button() 重新应用到 标签,另一个将复选框转换为 iPhone 风格的开关。

I've created a refreshJQuery() function that applies all my beefy behaviors in one go. I then call this function when I subscribe to observables in my view model to update my model. And once on page load.

function refreshJQuery() {
    $(".draggable, .droppable, .resizable").each(function () {

        var $element = $(this);

        if ($element.hasClass("draggable") && !$element.data("init-draggable")) {
            $element.data("init-draggable", true).draggable(dragBehavior);
        }
        if ($element.hasClass("droppable") && !$element.data("init-droppable")) {
            $element.data("init-droppable", true).droppable(dropBehavior);
        }
        if ($element.hasClass("resizable") && !$element.data("init-resizable")) {
            $element.data("init-resizable", true).resizable(resizeBehavior);
            $(this).children().fadeTo(2000, 0);
            $element.hover(function () {
                $(this).children().fadeTo(200, 1);
            },
            function () {
                $(this).children().fadeTo(750, 0);
            });
        }
    });
};

I then use it as follows, though I've identified 2 other observables I need to call this function from also:

this.updateTime = function () {
    service.updateBookingTimes(this.id(), this.startDate(), this.endDate());
    refreshJQuery();
};

this.startDate.subscribe(this.updateTime, this);
this.endDate.subscribe(this.updateTime, this);

I've also implemented @RPNiemeyer's solution for field level JQuery subscriptions, so in addition to his sample for a datepicker, I have similar code which reapplies $.button() to <a> tags and another which converts checkboxes into iPhone style switches.

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