最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Infinite scroll with VueJS and vue-resource - Stack Overflow

programmeradmin1浏览0评论

So, I can't for the life of me figure out how to get a proper infinite scroll done with VueJS and vue-resource. My data is loading with VueJS and vue-resource, but the trigger on scroll and proper handling is the issue.

Does anybody know how to do this? All attempts I tried lead to loads of double requests and spamming my backend with repeat requests.

What I've tried so far:

  • Wrapping the "this.$http.get" request into a en event listener for window.scroll and a conditional within that, which checks if the bottom of the page is reached. This would alway double or even multi-trigger the get request instead of just one trigger and then waiting for load again.

  • Doing something similar but with an element at the very bottom of the list where I would check if it was in view. The same thing with multi-triggering get requests.

So, I can't for the life of me figure out how to get a proper infinite scroll done with VueJS and vue-resource. My data is loading with VueJS and vue-resource, but the trigger on scroll and proper handling is the issue.

Does anybody know how to do this? All attempts I tried lead to loads of double requests and spamming my backend with repeat requests.

What I've tried so far:

  • Wrapping the "this.$http.get" request into a en event listener for window.scroll and a conditional within that, which checks if the bottom of the page is reached. This would alway double or even multi-trigger the get request instead of just one trigger and then waiting for load again.

  • Doing something similar but with an element at the very bottom of the list where I would check if it was in view. The same thing with multi-triggering get requests.

Share Improve this question edited Oct 29, 2015 at 7:15 barfurth asked Oct 28, 2015 at 11:37 barfurthbarfurth 6617 silver badges17 bronze badges 2
  • Can you edit your post with what you have tried so far? – Thomas Kim Commented Oct 29, 2015 at 5:27
  • 1 You can set a boolean in the vue instance. isFetching = false;. Then, you only fetch data if isFetching is false. You set it to true when you're getting data and set it back to false once the data has been fetched. – Thomas Kim Commented Oct 30, 2015 at 3:48
Add a ment  | 

1 Answer 1

Reset to default 4

One solution would be to setup a locking mechanism to stop rapid requests to your backend. The lock would be enabled before a request is made, and then the lock would be disabled when the request has been pleted and the DOM has been updated with the new content (which extends the size of your page).

For example:

new Vue({
  // ... your Vue options.

  ready: function () {
    var vm = this;
    var lock = true;
    window.addEventListener('scroll', function () {
      if (endOfPage() && lock) {
        vm.$http.get('/myBackendUrl').then(function(response) {
          vm.myItems.push(response.data);
          lock = false;
        });
      };
    });
  };
});

Another thing to keep in mind is that the scroll event is triggered more than you really need it to (especially on mobile devices), and you can throttle this event for improved performance. This can efficiently be done with requestAnimationFrame:

;(function() {
    var throttle = function(type, name, obj) {
        obj = obj || window;
        var running = false;
        var func = function() {
            if (running) { return; }
            running = true;
            requestAnimationFrame(function() {
                obj.dispatchEvent(new CustomEvent(name));
                running = false;
            });
        };
        obj.addEventListener(type, func);
    };

    /* init - you can init any event */
    throttle ("scroll", "optimizedScroll");
})();

// handle event
window.addEventListener("optimizedScroll", function() {
    console.log("Resource conscious scroll callback!");
});

Source: https://developer.mozilla/en-US/docs/Web/Events/scroll#Example

发布评论

评论列表(0)

  1. 暂无评论