Migrated and fixed the rest of the views using the new loading system.

This commit is contained in:
Scott Erickson 2014-04-28 11:52:04 -07:00
parent 8164c6881c
commit 02a74ae40c
10 changed files with 28 additions and 56 deletions

View file

@ -1,6 +1,9 @@
CocoCollection = require 'collections/CocoCollection'
File = require 'models/File'
module.exports = class ModelFiles extends CocoCollection
model: File
constructor: (model) ->
super()
url = model.constructor.prototype.urlRoot

View file

@ -688,8 +688,11 @@
model: "Model"
system: "System"
component: "Component"
components: "Components"
thang: "Thang"
thangs: "Thangs"
level_session: "Your Session"
opponent_session: "Opponent Session"
article: "Article"
user_names: "User Names"
files: "Files"

View file

@ -2,5 +2,5 @@ CocoModel = require('./CocoModel')
module.exports = class File extends CocoModel
@className: "File"
@schema: require 'schemas/models/file'
@schema: {}
urlRoot: "/db/file"

View file

@ -80,6 +80,7 @@ module.exports = class SuperModel extends Backbone.Model
return _.values @models
registerModel: (model) ->
console.log 'register model?', model, model.getURL
@models[model.getURL()] = model
getCollection: (collection) ->
@ -111,7 +112,6 @@ module.exports = class SuperModel extends Backbone.Model
addModelResource: (modelOrCollection, name, fetchOptions, value=1) ->
modelOrCollection.saveBackups = @shouldSaveBackups(modelOrCollection)
@checkName(name)
@registerModel(modelOrCollection)
res = new ModelResource(modelOrCollection, name, fetchOptions, value)
@storeResource(res, value)
return res
@ -139,7 +139,7 @@ module.exports = class SuperModel extends Backbone.Model
@listenToOnce(resource, 'loaded', @onResourceLoaded)
@listenTo(resource, 'failed', @onResourceFailed)
@denom += value
@updateProgress()
@updateProgress() if @denom
onResourceLoaded: (r) ->
@num += r.value

View file

@ -17,15 +17,8 @@ module.exports = class ThangComponentEditView extends CocoView
@world = options.world
@level = options.level
@callback = options.callback
@componentCollection = @supermodel.getCollection new ComponentsCollection()
if not @componentCollection.loaded
@supermodel.addModelResource(@componentCollection, 'component_collection').load()
@componentCollection = @supermodel.loadCollection(new ComponentsCollection(), 'components').model
onLoaded: ->
@supermodel.addCollection @componentCollection
super()
afterRender: ->
super()
return unless @supermodel.finished()

View file

@ -22,10 +22,9 @@ module.exports = class AddThangsView extends View
constructor: (options) ->
super options
@world = options.world
@thangTypes = @supermodel.getCollection new ThangTypeSearchCollection() # should load depended-on Components, too
@thangTypesRes = @supermodel.addModelResource(@thangTypes, 'add_thang_type_search_collection')
@thangTypesRes.load()
# should load depended-on Components, too
@thangTypes = @supermodel.loadCollection(new ThangTypeSearchCollection(), 'thangs').model
getRenderData: (context={}) ->
context = super(context)

View file

@ -64,20 +64,13 @@ module.exports = class ThangsTabView extends View
super options
@world = options.world
@thangTypes = @supermodel.getCollection new ThangTypeSearchCollection() # should load depended-on Components, too
@supermodel.addModelResource(@thangTypes, 'thang_type_search_collection').load()
# should load depended-on Components, too
@thangTypes = @supermodel.loadCollection(new ThangTypeSearchCollection(), 'thangs').model
# just loading all Components for now: https://github.com/codecombat/codecombat/issues/405
@componentCollection = @supermodel.getCollection new ComponentsCollection()
@supermodel.addModelResource(@componentCollection, 'components_collection').load()
@componentCollection = @supermodel.loadCollection(new ComponentsCollection(), 'components').load()
$(document).bind 'contextmenu', @preventDefaultContextMenu
onLoaded: ->
@supermodel.addCollection @thangTypes
@supermodel.addCollection @componentCollection
super()
getRenderData: (context={}) ->
context = super(context)
return context unless @supermodel.finished()

View file

@ -47,25 +47,11 @@ module.exports = class ThangTypeEditView extends View
super options
@mockThang = $.extend(true, {}, @mockThang)
@thangType = new ThangType(_id: @thangTypeID)
@thangType = @supermodel.loadModel(@thangType, 'thang').model
@thangType.saveBackups = true
@listenToOnce(@thangType, 'error', =>
@hideLoading()
# Hack: editor components appear after calling insertSubView.
# So we need to hide them first.
$(@$el).find('.main-content-area').children('*').not('#error-view').remove()
@insertSubView(new ErrorView())
)
thangRes = @supermodel.addModelResource(@thangType, 'thang_type')
thangRes.load()
@files = new DocumentFiles(@thangType)
thangDocRes = @supermodel.addModelResource(@files, 'thang_document')
thangDocRes.load()
@listenToOnce @thangType, 'sync', ->
console.log 'files for?', @thangType.id, @thangType.get 'name'
@files = @supermodel.loadCollection(new DocumentFiles(@thangType), 'files').model
@refreshAnimation = _.debounce @refreshAnimation, 500
getRenderData: (context={}) ->
@ -81,9 +67,9 @@ module.exports = class ThangTypeEditView extends View
main = _.keys(@thangType.get('actions') or {})
main.concat(raw)
onLoaded: -> @render()
afterRender: ->
super()
return unless @supermodel.finished()
@initStage()
@buildTreema()
@initSliders()

View file

@ -37,7 +37,6 @@ module.exports = class LadderTabView extends CocoView
constructor: (options, @level, @sessions) ->
super(options)
@socialNetworkRes = @supermodel.addSomethingResource("social_network_apis", 0)
@teams = teamDataFromLevel @level
@leaderboards = {}
@refreshLadder()
@ -151,14 +150,14 @@ module.exports = class LadderTabView extends CocoView
# LADDER LOADING
refreshLadder: ->
@ladderLimit ?= parseInt @getQueryVariable('top_players', 20)
for team in @teams
@leaderboards[team.id]?.destroy()
teamSession = _.find @sessions.models, (session) -> session.get('team') is team.id
@leaderboards[team.id] = new LeaderboardData(@level, team.id, teamSession)
@leaderboards[team.id] = new LeaderboardData(@level, team.id, teamSession, @ladderLimit)
@leaderboardRes = @supermodel.addModelResource(@leaderboards[team.id], 'leaderboard', 3)
@leaderboardRes.load()
onLoaded: -> @render()
render: ->
super()
@ -284,8 +283,9 @@ class LeaderboardData extends CocoClass
Consolidates what you need to load for a leaderboard into a single Backbone Model-like object.
###
constructor: (@level, @team, @session) ->
constructor: (@level, @team, @session, @limit) ->
super()
@fetch()
fetch: ->
@topPlayers = new LeaderboardCollection(@level, {order:-1, scoreOffset: HIGHEST_SCORE, team: @team, limit: @limit})

View file

@ -36,13 +36,8 @@ module.exports = class LadderView extends RootView
constructor: (options, @levelID) ->
super(options)
@level = new Level(_id:@levelID)
@levelRes = @supermodel.addModelResource(@level, 'level')
@levelRes.load()
@sessions = new LevelSessionsCollection(levelID)
@sessionRes = @supermodel.addModelResource(@sessions, 'level_sessions_collection')
@sessionRes.load()
@level = @supermodel.loadModel(new Level(_id:@levelID), 'level').model
@sessions = @supermodel.loadCollection(new LevelSessionsCollection(levelID), 'your_sessions').model
@teams = []