Show progress bar while knockout is rendering the view

拟墨画扇 提交于 2019-11-29 20:33:30

问题


I have a complex page that uses knockout to render the contents via templates. It takes around 10 seconds to render so I want to show a progress bar while this happens. I have tried to add a callback in the template to the afterRender method which broke the page - I think this method is more to do with fiddling with the html generated by the template.

I have also tried creating a binding handler that updates the progress bar on every call:

            ko.bindingHandlers.updateProgressBar = {
                init: function (element, valueAccessor) {
                    viewModel.updateProgressBar();
                }
            };

...

<ul data-bind="template: {name: 'genericItemTemplate', foreach: ChildItems},  updateProgressBar: true"></ul>

Unfortunately, although the method does get called each time, the UI does not get updated until the templates have completely finished rendering so I don't get the running progress that I am looking for.

I am using tmpl template library.

How can I display update the UI with progress of the template working its way through a large collection of items in an observableArray??


回答1:


One choice is to place your initial data into a separate array to start with and then use it as a queue. You would splice "x" number of items from the temp array and push them to your real observableArray in a setTimeout.

You can then use a dependentObservable to track the percent complete.

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




回答2:


I've just fork that fiddle and add some style to make a fully functional progress bar, check it: http://jsfiddle.net/Pegazux/h3UuG/



来源:https://stackoverflow.com/questions/8227704/show-progress-bar-while-knockout-is-rendering-the-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!