KnockoutJS - 使用自定义绑定添加动画
我想利用自定义绑定来显示进度图标(微调器或栏),同时通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为处理这个问题的一种简单方法是将诸如
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, setloading
to true when you are starting your AJAX request. Setloading
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 whenprice
is updated. You could set theprice
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).