更改时自动刷新列表视图 - knockoutjs & jQuery 移动版
我正在将 knockoutjs(对它来说很新)与 JQuery Mobile 一起使用。我有一个将过滤结果绑定到的列表视图。第一次加载数据后,我必须调用
$('ul').listview('refresh');
JQM 来重新设计我的列表,这非常有效。
但是,当我过滤列表时,它会重新渲染并再次失去样式,并且我无法弄清楚在哪里再次调用刷新。
我的 html 如下:
<p>Filter: <input data-bind="value: filter, valueUpdate: 'afterkeydown'" /></p>
<ul data-role="listview" data-theme="g" data-bind="template: {name: 'myTemplate', foreach: filteredItems }" />
我的 Knockout JS 是:
var car = function (name, make, year) {
this.name = name;
this.make = make;
this.year = year;
}
var carsViewModel = {
cars: ko.observableArray([]),
filter: ko.observable()
};
//filter the items using the filter text
carsViewModel.filteredItems = ko.dependentObservable(function () {
var filter = this.filter();
if (!filter) {
return this.cars();
} else {
return ko.utils.arrayFilter(this.cars(), function (item) {
return item.make == filter;
});
}
}, carsViewModel);
function init() {
carsViewModel.cars.push(new car("car1", "bmw", 2000));
carsViewModel.cars.push(new car("car2", "bmw", 2000));
carsViewModel.cars.push(new car("car3", "toyota", 2000));
carsViewModel.cars.push(new car("car4", "toyota", 2000));
carsViewModel.cars.push(new car("car5", "toyota", 2000));
ko.applyBindings(carsViewModel);
//refresh the list to reapply the styles
$('ul').listview('refresh');
}
我确信我错过了一些非常愚蠢的东西...
谢谢您的时间。
I am using knockoutjs (very new to it) with JQuery Mobile. I have a listview which I bind filtered results to. After I load my data the first time I have to call
$('ul').listview('refresh');
in order for JQM to restyle my list, this works great.
However when I filter my list, it is rerendered and looses the style again and I can't figure out where to call the refresh again.
My html is as follows:
<p>Filter: <input data-bind="value: filter, valueUpdate: 'afterkeydown'" /></p>
<ul data-role="listview" data-theme="g" data-bind="template: {name: 'myTemplate', foreach: filteredItems }" />
My Knockout JS is:
var car = function (name, make, year) {
this.name = name;
this.make = make;
this.year = year;
}
var carsViewModel = {
cars: ko.observableArray([]),
filter: ko.observable()
};
//filter the items using the filter text
carsViewModel.filteredItems = ko.dependentObservable(function () {
var filter = this.filter();
if (!filter) {
return this.cars();
} else {
return ko.utils.arrayFilter(this.cars(), function (item) {
return item.make == filter;
});
}
}, carsViewModel);
function init() {
carsViewModel.cars.push(new car("car1", "bmw", 2000));
carsViewModel.cars.push(new car("car2", "bmw", 2000));
carsViewModel.cars.push(new car("car3", "toyota", 2000));
carsViewModel.cars.push(new car("car4", "toyota", 2000));
carsViewModel.cars.push(new car("car5", "toyota", 2000));
ko.applyBindings(carsViewModel);
//refresh the list to reapply the styles
$('ul').listview('refresh');
}
I am sure that there is something very silly that I am missing...
Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题已经在 KO 论坛上出现过几次了。
一种选择是创建一个绑定到您的
filteredItems
的绑定并运行列表视图刷新。它可能看起来像:
现在,您可以将其放置在容器上(或者实际上放置在任何元素上)并传入您希望它依赖的可观察对象,例如:
This issue has come up on the KO forums a few times.
One option is to create a binding that is bound to your
filteredItems
and runs the listview refresh.It could look like:
Now, you would place this on the container (or really on any element) and pass in the observable that you want it to depend on like:
您可以在 jsfiddle 上发布完整的工作代码吗?因为我遇到了同样的问题,并且尝试了您的解决方案,但仍然不起作用。
[编辑]:好的,它对我来说效果很好:
Can you post the entire working code on jsfiddle ? Because I am having the same issue and I tried your solution but it still not working.
[Edit] : Ok, it worked fine for me like this :
基于前两个答案,这里有一些更完整的内容。 并通过处理异常而不是超时来解决 jQM 页面生命周期后触发刷新的问题:
它允许您使用无容器绑定(即注释中的 foreach) , co.uk/blog/colin/2012/10/integrating-knockout-and-jquerymobile/" rel="nofollow">我的博客上的完整工作示例。希望有帮助!
Building on the previous two answers, here is something a little more complete. It allows you to use containerless binding (i.e. foreach within comments), and soles the issue of refresh being fired after the jQM page lifecycle by handling exceptions rather than a timeout:
There's a complete worked example up on my blog. Hope that helps!