Browse Source

Added error checking to AngularJS resources.

bmallred 13 years ago
parent
commit
87dfbd466d
3 changed files with 62 additions and 9 deletions
  1. 57 7
      armory.js
  2. 5 1
      index.html
  3. 0 1
      site.css

+ 57 - 7
armory.js

@ -18,15 +18,47 @@ window.WowArmory.ArmoryController = function($scope, $resource) {
18 18
    $scope.realm = { name: '', slug: '' };
19 19
    $scope.guild = '';
20 20
21
    // Resource for the realms.
22
    $scope.apiRealms = $resource('//:server/api/wow/realm/status',
23
        { server: 'us.battle.net', jsonp: window.WowArmory.NewCallback() },
24
        { get: { method: 'JSONP' }});
25
    $scope.realms = $scope.apiRealms.get();
21
    // Handle the realms and its resource(s).
22
    $scope.realmError = '';
23
    $scope.realms = [];
24
    $scope.fetchRealms = function(roundTrip) {
25
        if (roundTrip == undefined) {
26
            roundTrip = 0;
27
        }
28
        
29
        $scope.apiRealms = $resource('//:server/api/wow/realm/status',
30
            { server: 'us.battle.net', jsonp: window.WowArmory.NewCallback() },
31
            { get: { method: 'JSONP' }});
32
33
        $scope.realms = $scope.apiRealms.get(
34
            {},
35
            function () {
36
                $scope.realmError = '';    
37
            },
38
            function () {
39
                if (console) {
40
                    console.log('Error retrieving guilds from the remote server.');
41
                }
42
                
43
                if (++roundTrip < 3) {
44
                    $scope.realmError = 'Attempting to fetch realms [attempt ' + roundTrip + ' of 3].';
45
                    setTimeout(function() { $scope.fetchRealms(roundTrip); }, 3000);
46
                }
47
                else {
48
                    $scope.realmError = 'Exhausted all resources, battle.net sucks.';
49
                }
50
            });
51
    };
52
    $scope.fetchRealms(0);
26 53
27 54
    // Handle the members and its resource(s).
55
    $scope.memberError = '';
28 56
    $scope.members = [];
29
    $scope.fetchMembers = function() {
57
    $scope.fetchMembers = function(roundTrip) {
58
        if (roundTrip == undefined) {
59
            roundTrip = 0;
60
        }
61
30 62
        if ($scope.realm.slug.length > 0 && $scope.guild.length > 0) {
31 63
32 64
            // Resource for the guild members.
@ -34,7 +66,25 @@ window.WowArmory.ArmoryController = function($scope, $resource) {
34 66
                { server: 'us.battle.net', realm: $scope.realm.slug, guild: $scope.guild, fields: 'members', jsonp: window.WowArmory.NewCallback() }, 
35 67
                { get: { method: 'JSONP' }});
36 68
37
            $scope.members = $scope.apiMembers.get();
69
            $scope.members = $scope.apiMembers.get(
70
                {}, 
71
                function() {
72
                    $scope.memberError = '';    
73
                }, 
74
                function() {
75
                    if (console) {
76
                        console.log('Error retrieving information from remote server.');
77
                    }
78
                    
79
                    if (++roundTrip < 3) {
80
                        $scope.memberError = 'Attempting to fetch members [attempt ' + roundTrip + ' of 3].';
81
                        setTimeout(function() { $scope.fetchMembers(roundTrip); }, 3000);
82
                    }
83
                    else {
84
                        $scope.memberError = 'Could not find the guild or any members.';
85
                    }
86
                    
87
                });
38 88
        }
39 89
    };
40 90
};

+ 5 - 1
index.html

@ -18,7 +18,8 @@
18 18
        <label for="myGuild">Guild</label>
19 19
        <input id="myGuild" type="text" ng-model="guild" placeholder="Guild name" />
20 20
21
        <button type="submit" ng-click="fetchMembers()">Get Members</button>
21
        <p style="font-style: italic;">example: "Hellscream" : "Rejected"</p>
22
        <button type="submit" ng-click="fetchMembers()" ng-disabled="realmError.length > 0">Get Members</button>
22 23
        </form>
23 24
24 25
        <hr />
@ -31,6 +32,9 @@
31 32
        <hr />
32 33
33 34
        <h2>{{guild}}</h2>
35
        <p>{{realmError}}</p>
36
        <p>{{memberError}}</p>
37
34 38
        <div class="character" ng-repeat="member in members.members">
35 39
            <img class="thumbnail" ng:src="//us.battle.net/static-render/us/{{member.character.thumbnail}}" />
36 40

+ 0 - 1
site.css

@ -42,7 +42,6 @@ button, input, select {
42 42
}
43 43
44 44
.character .stats {
45
    float: left;
46 45
    text-align: left;
47 46
    vertical-align: text-top;
48 47
}