forked from chieffancypants/angular-loading-bar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (44 loc) · 1.81 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
angular.module('LoadingBarExample', ['chieffancypants.loadingBar', 'ngAnimate'])
.config(function(cfpLoadingBarProvider) {
// true is the default, but I left this here as an example:
cfpLoadingBarProvider.includeSpinner = true;
})
.controller('ExampleCtrl', function ($scope, $http, $timeout, cfpLoadingBar) {
$scope.posts = [];
$scope.section = null;
$scope.subreddit = null;
$scope.subreddits = ['cats', 'pics', 'funny', 'gaming', 'AdviceAnimals', 'aww'];
var getRandomSubreddit = function() {
var sub = $scope.subreddits[Math.floor(Math.random() * $scope.subreddits.length)];
// ensure we get a new subreddit each time.
if (sub == $scope.subreddit) {
return getRandomSubreddit();
}
return sub;
};
$scope.fetch = function() {
$scope.subreddit = getRandomSubreddit();
// see how there's no need to keep track of these XHR requests?
// the interceptor is doing all the work in the background
// your controllers and services don't need to know anything about it!
$http.jsonp('https://www.reddit.com/r/' + $scope.subreddit + '.json?limit=100&jsonp=JSON_CALLBACK').success(function(data) {
// we're requesting 100 entries just to exaggerate the loading bar's progress
// since this is just for an example, don't display all 100, just the first 5
var posts = data.data.children.slice(0,5);
$scope.posts = posts;
});
};
$scope.start = function() {
cfpLoadingBar.start();
};
$scope.complete = function () {
cfpLoadingBar.complete();
};
// fake the initial load so first time users can see the bar right away:
$scope.start();
$scope.fakeIntro = true;
$timeout(function() {
$scope.complete();
$scope.fakeIntro = false;
}, 1250);
});