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

View file

@ -31,7 +31,6 @@ module.exports = class Level extends CocoModel
visit = (system) -> visit = (system) ->
return if system.original of originalsSeen return if system.original of originalsSeen
systemModel = _.find systemModels, {original: system.original} systemModel = _.find systemModels, {original: system.original}
console.error "Couldn't find model for original", system.original, "from", systemModels unless systemModel console.error "Couldn't find model for original", system.original, "from", systemModels unless systemModel
for d in systemModel.dependencies or [] for d in systemModel.dependencies or []
system2 = _.find levelSystems, {original: d.original} 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) -> loadCollection: (collection, name, fetchOptions, value=1) ->
url = collection.getURL() url = collection.getURL()
if cachedCollection = @collections[url] if cachedCollection = @collections[url]
console.debug 'Collection cache hit', url, 'already loaded', cachedModel.loaded console.debug 'Collection cache hit', url, 'already loaded', cachedCollection.loaded
if cachedModel.loaded if cachedCollection.loaded
res = @addModelResource(cachedCollection, name, fetchOptions, 0) res = @addModelResource(cachedCollection, name, fetchOptions, 0)
res.markLoaded() res.markLoaded()
return res return res
@ -139,6 +139,7 @@ module.exports = class SuperModel extends Backbone.Model
@listenToOnce(resource, 'loaded', @onResourceLoaded) @listenToOnce(resource, 'loaded', @onResourceLoaded)
@listenTo(resource, 'failed', @onResourceFailed) @listenTo(resource, 'failed', @onResourceFailed)
@denom += value @denom += value
@updateProgress()
onResourceLoaded: (r) -> onResourceLoaded: (r) ->
@num += r.value @num += r.value
@ -219,9 +220,10 @@ class RequestResource extends Resource
load: -> load: ->
@markLoading() @markLoading()
@jqxhr = $.ajax(jqxhrOptions) @jqxhr = $.ajax(@jqxhrOptions)
@jqxhr.done @markLoaded() # make sure any other success/fail callbacks happen before resource loaded callbacks
@jqxhr.fail @markFailed() @jqxhr.done => _.defer => @markLoaded()
@jqxhr.fail => _.defer => @markFailed()
@ @

View file

@ -38,7 +38,7 @@ module.exports = class EditorLevelView extends View
super options super options
@supermodel.shouldSaveBackups = (model) -> @supermodel.shouldSaveBackups = (model) ->
model.constructor.className in ['Level', 'LevelComponent', 'LevelSystem'] 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 @level = @levelLoader.level
@files = new DocumentFiles(@levelLoader.level) @files = new DocumentFiles(@levelLoader.level)
@supermodel.loadCollection(@files, 'file_names') @supermodel.loadCollection(@files, 'file_names')

View file

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

View file

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