Skip to content

Commit

Permalink
added badger.getBadges().
Browse files Browse the repository at this point in the history
  • Loading branch information
toolness committed Oct 18, 2012
1 parent e319f75 commit 2453fd0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions clopenbadger.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ define(["jquery", "backbone-events"], function($, BackboneEvents) {
availableBadges: undefined,
earnedBadges: undefined,
unreadBadgeCount: undefined,
getBadges: function() {
var badges = [];
Object.keys(self.availableBadges).forEach(function(shortname) {
var badge = {};
var badgeTypeInfo = self.availableBadges[shortname];
var badgeInstanceInfo = self.earnedBadges[shortname] || {};
badge.isEarned = (shortname in self.earnedBadges);
badges.push($.extend(badge, badgeTypeInfo, badgeInstanceInfo));
});
return badges;
},
markAllBadgesAsRead: function() {
if (self.unreadBadgeCount == 0)
return;
Expand Down
30 changes: 30 additions & 0 deletions test/all-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,34 @@ defineTests([
});
badger.credit('LOGGED_IN');
});

test("getBadges() works for unearned badges", function() {
Server.flushResponses();
deepEqual(badger.getBadges(), [{
"behaviors": [{"name": "LOGGED_IN", "score": 1}],
"criteria": "Can log into a site that uses Persona for auth.",
"description": "Like a champion, you logged in...",
"image": "https://wiki.mozilla.org/images/b/bb/Merit-badge.png",
"name": "First Login",
"prerequisites": [],
"isEarned": false
}]);
});

test("getBadges() works for earned badges", function() {
badger.credit("LOGGED_IN");
Server.flushResponses();
deepEqual(badger.getBadges(), [{
"behaviors": [{"name": "LOGGED_IN", "score": 1}],
"criteria": "Can log into a site that uses Persona for auth.",
"description": "Like a champion, you logged in...",
"image": "https://wiki.mozilla.org/images/b/bb/Merit-badge.png",
"name": "First Login",
"prerequisites": [],
"isEarned": true,
"issuedOn": 12345,
"isRead": false,
"assertionUrl": "http://clopenbadger/foo@bar.org/FIRST_LOGIN",
}]);
});
});

0 comments on commit 2453fd0

Please sign in to comment.