Set up the patches view with the new loading system, fixing a few things in the meanwhile.

This commit is contained in:
Scott Erickson 2014-04-28 11:09:21 -07:00
parent 17a0bf54c6
commit 01f2a556f9
7 changed files with 29 additions and 54 deletions

View file

@ -29,6 +29,7 @@ module.exports = class LevelLoader extends CocoClass
@team = options.team
@headless = options.headless
@spectateMode = options.spectateMode ? false
@editorMode = options.editorMode # TODO: remove when the surface can load ThangTypes itself
@loadSession()
@loadLevel()
@ -55,15 +56,13 @@ module.exports = class LevelLoader extends CocoClass
url = "/db/level/#{@levelID}/session"
url += "?team=#{@team}" if @team
@session = new LevelSession()
@session.url = -> url
@supermodel.addModelResource(@session, 'level_session', {cache:false}).load()
@session = new LevelSession().setURL url
@supermodel.loadModel(@session, 'level_session', {cache:false})
@session.once 'sync', -> @url = -> '/db/level.session/' + @id
if @opponentSessionID
@opponentSession = new LevelSession()
@opponentSession.url = "/db/level_session/#{@opponentSessionID}"
@supermodel.addModelResource(@opponentSession, 'opponent_session').load()
@opponentSession = new LevelSession().setURL "/db/level_session/#{@opponentSessionID}"
@supermodel.loadModel(@opponentSession, 'opponent_session')
# Supermodel (Level) Loading
@ -72,7 +71,7 @@ module.exports = class LevelLoader extends CocoClass
if @level.loaded
@populateLevel()
else
@supermodel.addModelResource(@level, 'level').load()
@level = @supermodel.loadModel(@level, 'level').model
@listenToOnce @level, 'sync', @onLevelLoaded
onLevelLoaded: ->
@ -102,7 +101,7 @@ module.exports = class LevelLoader extends CocoClass
for thangID in _.uniq thangIDs
url = "/db/thang.type/#{thangID}/version"
# url += "?project=true" if @headless
url += "?project=true" if @headless and not @editorMode
res = @maybeLoadURL url, ThangType, 'thang'
@listenToOnce res.model, 'sync', @buildSpriteSheetsForThangType if res
for obj in objUniq componentVersions
@ -119,14 +118,12 @@ module.exports = class LevelLoader extends CocoClass
@maybeLoadURL url, Level, 'level'
wizard = ThangType.loadUniversalWizard()
@supermodel.registerModel wizard
@supermodel.addModelResource(wizard, 'thang').load() if wizard.loading
@supermodel.loadModel wizard, 'thang'
maybeLoadURL: (url, Model, resourceName) ->
return if @supermodel.getModel(url)
model = new Model()
model.url = url
@supermodel.addModelResource(model, resourceName).load()
model = new Model().setURL url
@supermodel.loadModel(model, resourceName)
onSupermodelLoaded: ->
@loadLevelSounds()

View file

@ -31,7 +31,6 @@ module.exports = class Level extends CocoModel
visit = (system) ->
return if system.original of originalsSeen
systemModel = _.find systemModels, {original: system.original}
console.error "Couldn't find model for original", system.original, "from", systemModels unless systemModel
for d in systemModel.dependencies or []
system2 = _.find levelSystems, {original: d.original}

View file

@ -37,8 +37,8 @@ module.exports = class SuperModel extends Backbone.Model
loadCollection: (collection, name, fetchOptions, value=1) ->
url = collection.getURL()
if cachedCollection = @collections[url]
console.debug 'Collection cache hit', url, 'already loaded', cachedModel.loaded
if cachedModel.loaded
console.debug 'Collection cache hit', url, 'already loaded', cachedCollection.loaded
if cachedCollection.loaded
res = @addModelResource(cachedCollection, name, fetchOptions, 0)
res.markLoaded()
return res
@ -139,6 +139,7 @@ module.exports = class SuperModel extends Backbone.Model
@listenToOnce(resource, 'loaded', @onResourceLoaded)
@listenTo(resource, 'failed', @onResourceFailed)
@denom += value
@updateProgress()
onResourceLoaded: (r) ->
@num += r.value
@ -219,9 +220,10 @@ class RequestResource extends Resource
load: ->
@markLoading()
@jqxhr = $.ajax(jqxhrOptions)
@jqxhr.done @markLoaded()
@jqxhr.fail @markFailed()
@jqxhr = $.ajax(@jqxhrOptions)
# make sure any other success/fail callbacks happen before resource loaded callbacks
@jqxhr.done => _.defer => @markLoaded()
@jqxhr.fail => _.defer => @markFailed()
@

View file

@ -38,7 +38,7 @@ module.exports = class EditorLevelView extends View
super options
@supermodel.shouldSaveBackups = (model) ->
model.constructor.className in ['Level', 'LevelComponent', 'LevelSystem']
@levelLoader = new LevelLoader supermodel: @supermodel, levelID: @levelID, headless: true
@levelLoader = new LevelLoader supermodel: @supermodel, levelID: @levelID, headless: true, editorMode: true
@level = @levelLoader.level
@files = new DocumentFiles(@levelLoader.level)
@supermodel.loadCollection(@files, 'file_names')

View file

@ -20,16 +20,17 @@ module.exports = class PatchesView extends CocoView
initPatches: ->
@startedLoading = false
@patches = new PatchesCollection([], {}, @model, @status)
@patchesRes = @supermodel.addModelResource(@patches, 'patches')
@patchesRes.load()
@listenTo(@patchesRes, 'loaded', @load)
load: ->
return unless @patchesRes.loaded
console.log 'load patches view?'
@initPatches()
@patches = @supermodel.loadCollection(@patches, 'patches').model
@listenTo @patches, 'sync', @onPatchesLoaded
onPatchesLoaded: ->
ids = (p.get('creator') for p in @patches.models)
jqxhrOptions = nameLoader.loadNames ids
@nameLoaderRes = @supermodel.addRequestResource('name_loader', jqxhrOptions)
@nameLoaderRes.load()
@supermodel.addRequestResource('name_loader', jqxhrOptions).load() if jqxhrOptions
getRenderData: ->
c = super()
@ -38,7 +39,6 @@ module.exports = class PatchesView extends CocoView
c.status
c
onLoaded: -> @render()
afterRender: ->
@$el.find(".#{@status}").addClass 'active'
@ -47,9 +47,7 @@ module.exports = class PatchesView extends CocoView
@reloadPatches()
reloadPatches: ->
@loaded = false
@initPatches()
# @load()
@load()
@render()
openPatchModal: (e) ->

View file

@ -33,7 +33,7 @@ module.exports = class CocoView extends Backbone.View
constructor: (options) ->
@loadProgress = _.cloneDeep @loadProgress
@supermodel ?= new SuperModel()
@options = options
if options?.supermodel # kind of a hacky way to get each view to store its own progress
@supermodel.models = options.supermodel.models
@supermodel.collections = options.supermodel.collections
@ -49,7 +49,7 @@ module.exports = class CocoView extends Backbone.View
@toggleModal = _.debounce @toggleModal, 100
# Backbone.Mediator handles subscription setup/teardown automatically
@listenToOnce(@supermodel, 'loaded-all', @onLoaded)
@listenTo(@supermodel, 'loaded-all', @onLoaded)
@listenTo(@supermodel, 'update-progress', @updateProgress)
@listenTo(@supermodel, 'failed', @onResourceLoadFailed)

View file

@ -1,21 +0,0 @@
CocoView = require 'views/kinds/CocoView'
template = require 'templates/supermodel.demo'
User = require 'models/User'
module.exports = class UnnamedView extends CocoView
id: "supermodel-demo-view"
template: template
constructor: (options) ->
super(options)
@load()
load: ->
@supermodel.addModelResource(new User(me.id))
# getRenderData: ->
# c = super()
# c
# destroy: ->
# super()