diff --git a/app/lib/LevelLoader.coffee b/app/lib/LevelLoader.coffee index 5e1287109..5063982d9 100644 --- a/app/lib/LevelLoader.coffee +++ b/app/lib/LevelLoader.coffee @@ -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() diff --git a/app/models/Level.coffee b/app/models/Level.coffee index cff521d28..0a56ccc09 100644 --- a/app/models/Level.coffee +++ b/app/models/Level.coffee @@ -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} diff --git a/app/models/SuperModel.coffee b/app/models/SuperModel.coffee index ebc2caec6..aeef1712f 100644 --- a/app/models/SuperModel.coffee +++ b/app/models/SuperModel.coffee @@ -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() @ diff --git a/app/views/editor/level/edit.coffee b/app/views/editor/level/edit.coffee index 60d533750..ed48d7d5e 100644 --- a/app/views/editor/level/edit.coffee +++ b/app/views/editor/level/edit.coffee @@ -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') diff --git a/app/views/editor/patches_view.coffee b/app/views/editor/patches_view.coffee index 0ecbd9cb6..3c3a933e6 100644 --- a/app/views/editor/patches_view.coffee +++ b/app/views/editor/patches_view.coffee @@ -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) -> diff --git a/app/views/kinds/CocoView.coffee b/app/views/kinds/CocoView.coffee index 29cc9ecc6..b78e7b165 100644 --- a/app/views/kinds/CocoView.coffee +++ b/app/views/kinds/CocoView.coffee @@ -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) diff --git a/app/views/supermodel.demo.coffee b/app/views/supermodel.demo.coffee deleted file mode 100644 index b861791f3..000000000 --- a/app/views/supermodel.demo.coffee +++ /dev/null @@ -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() \ No newline at end of file