This demo shows a simple table, with data coming from in-page mock backend, where each request is slowed down by 1 second. The received data is stored in the angular $scope array, bound to the table. The demo works as static page, since everything is compiled / runs locally.
Loading the data
The controller function attached to the page just calls the backend url, keeping track of the next slice of data to request
1 | $scope.fetch = function () { |
Making fetch request on scroll
As the user scrolls to the bottom of the window, I use ng-infinite-scroll directive to signal and fetch more data.
1 | <tbody infinite-scroll="fetch()" |
For details how mock data is generated and $http request is intercepted see the index.html source.
Serving mock data
Having to run back server to return data is a big requirement. Luckily just to show that the frontend is working, we could intercept AJAX requests in-page using ngMockE2E.$httpBackend class.
Whenever a request is intercepted (by matching it to a RegExp to account for changing range indices), we generate an array of fake data using Faker.js and return it:
1 | angular.module('tester', ['infinite-fake-data', 'ngMockE2E']) |
Slowing down mock respones
Returning mock data almost instantenously from in-page mock does not reflect how the directive behaves in realistic network conditions: requests take time, depending on the conditions. To properly mock the network delays, I slowed down mock backend responses.
I have wrapped ngMockE2E.$httpBackend with a proxy object that delays executing the callback function (and thus transmitting the result to the client code) using the approach described in Endless Indirection
1 | .config(function ($provide) { |