Migrate view to fit new supermodel.

This commit is contained in:
Ting-Kuan 2014-04-16 02:28:59 -04:00
parent e58a5ee4b5
commit 52a73ceae3
11 changed files with 65 additions and 57 deletions

View file

@ -15,12 +15,11 @@ module.exports = class SuperModel extends Backbone.Model
@mustPopulate = model
model.saveBackups = @shouldSaveBackups(model)
url = model.url()
@models[url] = model unless @models[url]?
@addModel(model)
@modelLoaded(model) if model.loaded
resName = url unless resName
modelRes = @addModelResource(model, url)
resName = model.url unless resName
modelRes = @addModelResource(model, model.url)
schema = model.schema()
@schemas[schema.urlRoot] = schema
@ -68,9 +67,7 @@ module.exports = class SuperModel extends Backbone.Model
return _.values @models
addModel: (model) ->
url = model.url()
return console.warn "Tried to add Model '#{url}' to SuperModel, but it wasn't loaded." unless model.loaded
#return console.warn "Tried to add Model '#{url}' to SuperModel when we already had it." if @models[url]?
url = model.url
@models[url] = model
getCollection: (collection) ->
@ -97,9 +94,9 @@ module.exports = class SuperModel extends Backbone.Model
finished: ->
return @progress is 1.0 or Object.keys(@resources).length is 0
addModelResource: (modelOrCollection, name, fetchOptions, value=1) ->
@checkName(name)
@addModel(modelOrCollection)
res = new ModelResource(modelOrCollection, name, fetchOptions, value)
@storeResource(res, value)
return res
@ -167,6 +164,7 @@ module.exports = class SuperModel extends Backbone.Model
@num += r.value
@progress = @num / @denom
console.debug 'gintau', 'super-progress' , @progress, @num, @denom
@trigger('superModel:updateProgress', @progress)
@trigger('loaded-all') if @finished()
@ -219,7 +217,7 @@ class ModelResource extends Resource
@loadDeferred = $.Deferred()
$.when.apply($, @loadDependencies())
.then(@onLoadDependenciesSuccess, @onLoadDependenciesFailed)
.always(()=> @isLoading = false)
.always(=> @isLoading = false)
return @loadDeferred.promise()
@ -235,6 +233,11 @@ class ModelResource extends Resource
onLoadDependenciesSuccess: =>
@model.fetch(@fetchOptions)
# Hack: some components seem cannot be downloaded,
# so we need to complete fetching manually after a while if no error occurs.
if @model.constructor.className is 'ComponentsCollection'
setTimeout((=> @markLoaded() if @isLoading), 5000)
@listenToOnce(@model, 'sync', ->
@markLoaded()
@loadDeferred.resolve(@)

View file

@ -23,6 +23,7 @@ module.exports = class ThangComponentEditView extends CocoView
@listenToOnce(@componentCollectionRes, 'resource:loaded', @onComponentsSync)
@componentCollectionRes.load()
onLoaded: ->
afterRender: ->
super()
@buildExtantComponentTreema()
@ -31,7 +32,7 @@ module.exports = class ThangComponentEditView extends CocoView
onComponentsSync: (res) ->
return if @destroyed
@supermodel.addCollection @componentCollection
@render()
@hideLoading()
buildExtantComponentTreema: ->
level = new Level()

View file

@ -22,13 +22,10 @@ module.exports = class AddThangsView extends View
constructor: (options) ->
super options
@world = options.world
@thangTypes = @supermodel.getCollection new ThangTypeSearchCollection() # should load depended-on Components, too
@listenToOnce(@thangTypes, 'sync', @onThangTypesLoaded)
@thangTypes.fetch()
onThangTypesLoaded: ->
return if @destroyed
@render() # do it again but without the loading screen
@thangTypesRes = @supermodel.addModelResource(@thangTypes, 'add_thang_type_search_collection')
@thangTypesRes.load()
getRenderData: (context={}) ->
context = super(context)
@ -59,7 +56,6 @@ module.exports = class AddThangsView extends View
context
afterRender: ->
return if @startsLoading
super()
runSearch: (e) =>

View file

@ -22,6 +22,7 @@ module.exports = class ComponentsTabView extends View
events:
'click #create-new-component-button': 'createNewLevelComponent'
onLoaded: ->
onLevelThangsChanged: (e) ->
thangsData = e.thangsData
presentComponents = {}

View file

@ -27,7 +27,7 @@ module.exports = class EditorLevelView extends View
'click #commit-level-start-button': 'startCommittingLevel'
'click #fork-level-start-button': 'startForkingLevel'
'click #history-button': 'showVersionHistory'
'click #patches-tab': -> @patchesView.load()
'click #patches-tab': -> @patchesView?.load()
'click #commit-level-patch-button': 'startPatchingLevel'
constructor: (options, @levelID) ->
@ -45,32 +45,30 @@ module.exports = class EditorLevelView extends View
@supermodel.shouldSaveBackups = (model) ->
model.constructor.className in ['Level', 'LevelComponent', 'LevelSystem']
@worldRes = @supermodel.addSomethingResource('world')
@level = new Level _id: @levelID
@listenToOnce(@level, 'sync', @onLevelLoaded)
@listenToOnce(@supermodel, 'error',
() =>
#@listenToOnce(@level, 'sync', @onLevelLoaded)
@listenToOnce(@supermodel, 'error', =>
@hideLoading()
@insertSubView(new ErrorView())
)
@levelRes = @supermodel.populateModel(@level, 'level')
@levelRes = @supermodel.addModelResource(@level, 'level')
@listenToOnce(@levelRes, 'resource:loaded', ->
console.debug 'gintau', 'init-world'
@world = new World @level.name
@worldRes.markLoaded()
)
@levelRes.load()
@files = new DocumentFiles(@level)
@supermodel.addModelResource(@files, 'level_document').load()
showLoading: ($el) ->
$el ?= @$el.find('.tab-content')
super($el)
onLevelLoaded: ->
@files = new DocumentFiles(@level)
@supermodel.addModelResource(@files, 'level_document').load()
onAllLoaded: ->
@level.unset('nextLevel') if _.isString(@level.get('nextLevel'))
@initWorld()
# @render() # do it again but without the loading screen
initWorld: ->
@world = new World @level.name
getRenderData: (context={}) ->
context = super(context)
context.level = @level
@ -80,7 +78,8 @@ module.exports = class EditorLevelView extends View
afterRender: ->
super()
new LevelSystem # temp; trigger the LevelSystem schema to be loaded, if it isn't already
return unless @world and @level
console.debug 'gintau', 'edit-afterRender'
@$el.find('a[data-toggle="tab"]').on 'shown.bs.tab', (e) =>
Backbone.Mediator.publish 'level:view-switched', e
@thangsTab = @insertSubView new ThangsTabView world: @world, supermodel: @supermodel

View file

@ -16,7 +16,8 @@ module.exports = class ScriptsTabView extends View
super options
@world = options.world
@files = options.files
onLoaded: ->
onLevelLoaded: (e) ->
@level = e.level
@dimensions = @level.dimensions()

View file

@ -22,6 +22,7 @@ module.exports = class SettingsTabView extends View
super options
@world = options.world
onLoaded: ->
onLevelLoaded: (e) ->
@level = e.level
data = _.pick @level.attributes, (value, key) => key in @editableSettings

View file

@ -31,15 +31,17 @@ module.exports = class SystemsTabView extends View
url = "/db/level.system/#{system.original}/version/#{system.majorVersion}"
ls = new LevelSystem()
ls.saveBackups = true
lsRes = @supermodel.addModelResource(ls, 'level_system_' + system.original);
do (url) -> ls.url = -> url
continue if @supermodel.getModelByURL ls.url
ls.fetch()
@listenToOnce ls, 'sync', @onSystemLoaded
lsRes.load()
@listenToOnce(lsRes, 'resource:loaded', @onSystemLoaded)
++@toLoad
@onDefaultSystemsLoaded() unless @toLoad
onSystemLoaded: (ls) ->
@supermodel.addModel ls
onSystemLoaded: (lsRes) ->
ls = lsRes.model
@supermodel.addModel(ls)
--@toLoad
@onDefaultSystemsLoaded() unless @toLoad
@ -48,9 +50,9 @@ module.exports = class SystemsTabView extends View
@render() # do it again but without the loading screen
@onLevelLoaded level: @level if @level
onLoaded: ->
onLevelLoaded: (e) ->
@level = e.level
return if @startsLoading
@buildSystemsTreema()
buildSystemsTreema: ->
@ -145,9 +147,7 @@ class LevelSystemNode extends TreemaObjectNode
grabDBComponent: ->
unless _.isString @data.original
return alert('Press the "Add System" button at the bottom instead of the "+". Sorry.')
@system = @settings.supermodel.getModelByOriginalAndMajorVersion LevelSystem, @data.original, @data.majorVersion
#@system = _.find @settings.supermodel.getModels(LevelSystem), (m) =>
# m.get('original') is @data.original and m.get('version').major is @data.majorVersion
@system = @settings.supermodel.getModelByOriginalAndMajorVersion(LevelSystem, @data.original, @data.majorVersion)
console.error "Couldn't find system for", @data.original, @data.majorVersion, "from models", @settings.supermodel.models unless @system
getChildSchema: (key) ->

View file

@ -50,6 +50,7 @@ module.exports = class LevelThangEditView extends View
input.val(thangTypeName)
@$el.find('#thang-type-link span').text(thangTypeName)
window.input = input
@hideLoading()
saveThang: (e) ->
# Make sure it validates first?

View file

@ -60,31 +60,34 @@ module.exports = class ThangsTabView extends View
constructor: (options) ->
super options
@world = options.world
@thangTypes = @supermodel.getCollection new ThangTypeSearchCollection() # should load depended-on Components, too
@listenToOnce(@thangTypes, 'sync', @onThangTypesLoaded)
@thangTypes.fetch()
@thangTypesRes = @supermodel.addModelResource(@thangTypes, 'thang_type_search_collection')
@listenToOnce(@thangTypesRes, 'resource:loaded', @onThangTypesLoaded)
@thangTypesRes.load()
$(document).bind 'contextmenu', @preventDefaultContextMenu
# just loading all Components for now: https://github.com/codecombat/codecombat/issues/405
@componentCollection = @supermodel.getCollection new ComponentsCollection()
@listenToOnce(@componentCollection, 'sync', @onComponentsLoaded)
@componentCollection.fetch()
@componentCollectionRes = @supermodel.addModelResource(@componentCollection, 'components_collection')
@listenToOnce(@componentCollectionRes, 'resource:loaded', @onComponentsLoaded)
@componentCollectionRes.load()
onThangTypesLoaded: ->
return if @destroyed
@supermodel.addCollection @thangTypes
for model in @thangTypes.models
@supermodel.populateModel(model, model.name)
@startsLoading = not @componentCollection.loaded
@render() # do it again but without the loading screen
@onLevelLoaded level: @level if @level and not @startsLoading
# @render() # do it again but without the loading screen
# @onLevelLoaded level: @level if @level and not @startsLoading
onComponentsLoaded: ->
return if @destroyed
@supermodel.addCollection @componentCollection
@startsLoading = not @thangTypes.loaded
@render() # do it again but without the loading screen
@onLevelLoaded level: @level if @level and not @startsLoading
# @render() # do it again but without the loading screen
# @onLevelLoaded level: @level if @level and not @startsLoading
getRenderData: (context={}) ->
context = super(context)
@ -115,8 +118,8 @@ module.exports = class ThangsTabView extends View
oldHeight = $('#thangs-list').height()
$('#thangs-list').height(oldHeight - thangsHeaderHeight - 80)
onLoaded: ->
afterRender: ->
return if @startsLoading
super()
$('.tab-content').click @selectAddThang
$('#thangs-list').bind 'mousewheel', @preventBodyScrollingInThangList
@ -137,8 +140,9 @@ module.exports = class ThangsTabView extends View
e.preventDefault()
onLevelLoaded: (e) ->
console.debug 'gintau', 'thangs-tab-view', 'onLevelLoaded'
@level = e.level
return if @startsLoading
data = $.extend(true, {}, @level.attributes)
treemaOptions =
schema: Level.schema.properties.thangs
@ -153,6 +157,7 @@ module.exports = class ThangsTabView extends View
thang: ThangNode
array: ThangsNode
world: @world
@thangsTreema = @$el.find('#thangs-treema').treema treemaOptions
@thangsTreema.build()
@thangsTreema.open()

View file

@ -84,6 +84,7 @@ module.exports = class CocoView extends Backbone.View
render: ->
return @ unless me
super()
console.debug 'gintau', 'CocoView-Render', @
return @template if _.isString(@template)
@$el.html @template(@getRenderData())
@ -133,7 +134,6 @@ module.exports = class CocoView extends Backbone.View
onRetryResource: (e) ->
res = @supermodel.getResource($(e.target).data('resource-index'))
console.debug 'gintau', 'retry-resource', res
res.load()
$(e.target).closest('.loading-error-alert').remove()