KnockoutJS - 使用自定义绑定添加动画

发布于 2025-01-01 10:33:04 字数 1280 浏览 0 评论 0原文

我想利用自定义绑定来显示进度图标(微调器或栏),同时通过 ajax 调用更新价格。

在开始使用 Knockout 之前,我只是在 ajax 方法开始时调用 onUpdating() ,然后在 ajax 成功返回价格时调用 onUpdated(price) 。

function onUpdating() {
    $(".price").empty().html('<img src="/Content/Img/Site/progress.gif" />');
}

function onUpdated(price) {
    $(".price").empty().html('£' + price);
}

由于我现在使用 Knockout,我想使用自定义绑定,但我无法理解它是如何工作的。

我猜是这样的:

    ko.bindingHandlers.showProgress = {
        init: function (element, valueAccessor) {
            $(element).empty().html('<img src="/Content/Img/Site/progress.gif" />');
        },
        update: function (element, valueAccessor) {
            $(element).empty().html('£' + valueAccessor());
        }
    }

<p>
    Price: <span data-bind="showProgress: price"></span>
</p>

但是,这根本不起作用。非常感谢任何帮助。

编辑 以下内容似乎已实现,但更新时未显示进度条:

    ko.bindingHandlers.showProgress = {
        update: function (element, valueAccessor) {
            $(element).empty().html('<img src="/Content/Img/Site/progress.gif" />');
            var value = ko.utils.unwrapObservable(valueAccessor());
            $(element).empty().html('£' + value);
        }
    }

I'd like to make use of custom bindings to display a progress icon (spinner or bar) whilst a price is being updated via an ajax call.

Before I started using Knockout I just called onUpdating() at the start of my ajax method and then onUpdated(price) when the ajax successfully returned a price.

function onUpdating() {
    $(".price").empty().html('<img src="/Content/Img/Site/progress.gif" />');
}

function onUpdated(price) {
    $(".price").empty().html('£' + price);
}

As I'm now using Knockout I'd like to use the custom bindings but I just cant get my head around how it works.

I guess something like this:

    ko.bindingHandlers.showProgress = {
        init: function (element, valueAccessor) {
            $(element).empty().html('<img src="/Content/Img/Site/progress.gif" />');
        },
        update: function (element, valueAccessor) {
            $(element).empty().html('£' + valueAccessor());
        }
    }

<p>
    Price: <span data-bind="showProgress: price"></span>
</p>

However, this doesn't work at all. Any help greatly appreciated.

EDIT
The following seems to be getting there but its not showing the progress bar whilst updating:

    ko.bindingHandlers.showProgress = {
        update: function (element, valueAccessor) {
            $(element).empty().html('<img src="/Content/Img/Site/progress.gif" />');
            var value = ko.utils.unwrapObservable(valueAccessor());
            $(element).empty().html('£' + value);
        }
    }

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

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

发布评论

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

评论(1

差↓一点笑了 2025-01-08 10:33:04

我认为处理这个问题的一种简单方法是将诸如 loading 子可观察对象添加到您正在更新的可观察对象中。然后,在启动 AJAX 请求时将 loading 设置为 true。当数据到达时,将 loading 设置为 false。

然后,您可以将图像的可见性绑定到yourobservable.loading

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

另外,还有一个关于您的自定义绑定:只有当任何访问其值的可观察对象更新时,绑定的 update 函数才会再次运行。因此,在您的情况下,它只会在 price 更新时运行。您可以在 AJAX 请求之前将 price 设置为 null,然后在您的绑定中,当该值为 null 时显示加载图像,否则显示您的可观察值的实际值(当您的 AJAX 请求时您会更新该值)请求成功)。

I think that an easy way to handle this one is to add something like a loading sub-observable to the observable that you are updating. Then, set loading to true when you are starting your AJAX request. Set loading to false when your data arrives.

Then, you can bind the visibility of your image to yourobservable.loading.

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

Also, one tip about your custom bindings: The binding's update function will only run again when any of the observables that have their value accessed are updated. So, in your case it would only run when price is updated. You could set the price to null before your AJAX request, then in your binding show the loading image when the value is null, otherwise show the actual value of your observable (which you would have updated when your AJAX request succeeded).

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