This commit is contained in:
Scott Erickson 2014-06-06 16:46:42 -07:00
parent d00c4c2af3
commit f0cdf6b2e2
3 changed files with 32 additions and 5 deletions

View file

@ -36,10 +36,10 @@ module.exports = class LadderTabView extends CocoView
constructor: (options, @level, @sessions) -> constructor: (options, @level, @sessions) ->
super(options) super(options)
@socialNetworkRes = @supermodel.addSomethingResource("social_network_apis", 0)
@teams = teamDataFromLevel @level @teams = teamDataFromLevel @level
@leaderboards = {} @leaderboards = {}
@refreshLadder() @refreshLadder()
@socialNetworkRes = @supermodel.addSomethingResource("social_network_apis", 0)
@checkFriends() @checkFriends()
checkFriends: -> checkFriends: ->
@ -159,7 +159,7 @@ module.exports = class LadderTabView extends CocoView
oldLeaderboard.destroy() oldLeaderboard.destroy()
teamSession = _.find @sessions.models, (session) -> session.get('team') is team.id teamSession = _.find @sessions.models, (session) -> session.get('team') is team.id
@leaderboards[team.id] = new LeaderboardData(@level, team.id, teamSession, @ladderLimit) @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() @leaderboardRes.load()
render: -> render: ->
@ -286,16 +286,16 @@ module.exports = class LadderTabView extends CocoView
@ladderLimit += 100 @ladderLimit += 100
@refreshLadder() @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. Consolidates what you need to load for a leaderboard into a single Backbone Model-like object.
### ###
constructor: (@level, @team, @session, @limit) -> constructor: (@level, @team, @session, @limit) ->
super() super()
@fetch()
fetch: -> fetch: ->
console.warn 'Already have top players on', @ if @topPlayers
@topPlayers = new LeaderboardCollection(@level, {order:-1, scoreOffset: HIGHEST_SCORE, team: @team, limit: @limit}) @topPlayers = new LeaderboardCollection(@level, {order:-1, scoreOffset: HIGHEST_SCORE, team: @team, limit: @limit})
promises = [] promises = []
promises.push @topPlayers.fetch() promises.push @topPlayers.fetch()
@ -315,8 +315,9 @@ class LeaderboardData extends CocoClass
@promise @promise
onLoad: => onLoad: =>
return if @destroyed return if @destroyed or not @topPlayers.loaded
@loaded = true @loaded = true
@loading = false
@trigger 'sync', @ @trigger 'sync', @
# TODO: cache user ids -> names mapping, and load them here as needed, # 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. # and apply them to sessions. Fetching each and every time is too costly.

View file

@ -0,0 +1,8 @@
module.exports.LadderLevel =
name: 'Ladder Level'
original: '0123456789abcdef'
version:
major: 1
minor: 2
isLatestMajor: true
isLatestMinor: true

View file

@ -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)