diff --git a/app/views/play/ladder/ladder_tab.coffee b/app/views/play/ladder/ladder_tab.coffee index f979ee005..974d03ecd 100644 --- a/app/views/play/ladder/ladder_tab.coffee +++ b/app/views/play/ladder/ladder_tab.coffee @@ -36,10 +36,10 @@ module.exports = class LadderTabView extends CocoView constructor: (options, @level, @sessions) -> super(options) - @socialNetworkRes = @supermodel.addSomethingResource("social_network_apis", 0) @teams = teamDataFromLevel @level @leaderboards = {} @refreshLadder() + @socialNetworkRes = @supermodel.addSomethingResource("social_network_apis", 0) @checkFriends() checkFriends: -> @@ -159,7 +159,7 @@ module.exports = class LadderTabView extends CocoView oldLeaderboard.destroy() teamSession = _.find @sessions.models, (session) -> session.get('team') is team.id @leaderboards[team.id] = new LeaderboardData(@level, team.id, teamSession, @ladderLimit) - @leaderboardRes = @supermodel.addModelResource(@leaderboards[team.id], 'leaderboard', 3) + @leaderboardRes = @supermodel.addModelResource(@leaderboards[team.id], 'leaderboard', {}, 3) @leaderboardRes.load() render: -> @@ -286,16 +286,16 @@ module.exports = class LadderTabView extends CocoView @ladderLimit += 100 @refreshLadder() -class LeaderboardData extends CocoClass +module.exports.LeaderboardData = LeaderboardData = class LeaderboardData extends CocoClass ### Consolidates what you need to load for a leaderboard into a single Backbone Model-like object. ### constructor: (@level, @team, @session, @limit) -> super() - @fetch() fetch: -> + console.warn 'Already have top players on', @ if @topPlayers @topPlayers = new LeaderboardCollection(@level, {order:-1, scoreOffset: HIGHEST_SCORE, team: @team, limit: @limit}) promises = [] promises.push @topPlayers.fetch() @@ -315,8 +315,9 @@ class LeaderboardData extends CocoClass @promise onLoad: => - return if @destroyed + return if @destroyed or not @topPlayers.loaded @loaded = true + @loading = false @trigger 'sync', @ # TODO: cache user ids -> names mapping, and load them here as needed, # and apply them to sessions. Fetching each and every time is too costly. diff --git a/test/app/fixtures/levels.coffee b/test/app/fixtures/levels.coffee new file mode 100644 index 000000000..573fd5128 --- /dev/null +++ b/test/app/fixtures/levels.coffee @@ -0,0 +1,8 @@ +module.exports.LadderLevel = + name: 'Ladder Level' + original: '0123456789abcdef' + version: + major: 1 + minor: 2 + isLatestMajor: true + isLatestMinor: true \ No newline at end of file diff --git a/test/app/views/play/ladder/ladder_tab.spec.coffee b/test/app/views/play/ladder/ladder_tab.spec.coffee new file mode 100644 index 000000000..b809ebb31 --- /dev/null +++ b/test/app/views/play/ladder/ladder_tab.spec.coffee @@ -0,0 +1,18 @@ +LadderTabView = require 'views/play/ladder/ladder_tab' +Level = require 'models/Level' +fixtures = require 'test/app/fixtures/levels' + +describe 'LeaderboardData', -> + it 'triggers "sync" when its request is finished', -> + level = new Level(fixtures.LadderLevel) + leaderboard = new LadderTabView.LeaderboardData(level, 'humans', null, 4) + leaderboard.fetch() + + # no session passed in, so only one request + expect(jasmine.Ajax.requests.count()).toBe(1) + + request = jasmine.Ajax.requests.mostRecent() + triggered = false + leaderboard.once 'sync', -> triggered = true + request.response({status: 200, responseText: '{}'}) + expect(triggered).toBe(true) \ No newline at end of file