Backbone.js progress bar while fetching collection

前端 未结 6 653
既然无缘
既然无缘 2021-02-09 10:43

i\'d like to show a progress bar while i update the app with fresh content. I guess the best thing to do would be to do it while calling .fetch on a collection.

The cont

6条回答
  •  无人共我
    2021-02-09 11:23

    Try this:

    var SomeView = Backbone.View.extend({
        loadStuff: function(){
            var self = this;
            someModel.fetch({
                xhr: function() {
                    var xhr = $.ajaxSettings.xhr();
                    xhr.onprogress = self.handleProgress;
                    return xhr;
                }
            });
        },
        handleProgress: function(evt){
            var percentComplete = 0;
            if (evt.lengthComputable) {  
                percentComplete = evt.loaded / evt.total;
            }
            //console.log(Math.round(percentComplete * 100)+"%");
        }
    });
    

提交回复
热议问题