Change populate model to accept resource name.

This commit is contained in:
Ting-Kuan 2014-04-11 22:35:20 -04:00
parent e3a1f170cf
commit a6235842b6
5 changed files with 19 additions and 17 deletions

View file

@ -60,15 +60,15 @@ module.exports = class LevelLoader extends CocoClass
# Unless you specify cache:false, sometimes the browser will use a cached session
# and players will 'lose' code
@listenToOnce(@session, 'sync', @onSessionLoaded)
session_res = @supermodel.addModelResource(@session, @session.url, {cache:false})
session_res.load()
sessionRes = @supermodel.addModelResource(@session, 'level_session', {cache:false})
sessionRes.load()
if @opponentSessionID
@opponentSession = new LevelSession()
@opponentSession.url = "/db/level_session/#{@opponentSessionID}"
@listenToOnce(@opponentSession, 'sync', @onSessionLoaded)
opponentSession_res = @supermodel.addModelResource(@opponentSession, @opponentSession.url)
opponentSession_res.load()
opponentSessionRes = @supermodel.addModelResource(@opponentSession, 'opponent_session')
opponentSessionRes.load()
sessionsLoaded: ->
return true if @headless
@ -100,7 +100,7 @@ module.exports = class LevelLoader extends CocoClass
return true if headless and model.constructor.className is 'ThangType'
false
@supermodel.populateModel @level
@supermodel.populateModel(@level, 'level')
onSupermodelError: ->

View file

@ -619,3 +619,5 @@ module.exports = nativeDescription: "English", englishDescription: "English", tr
leaderboard: "Leaderboard"
user_schema: "User Schema"
user_profile: "User Profile"
level_session: "LevelSession"
opponent_session: "OpponentSession"

View file

@ -4,16 +4,15 @@ module.exports = class SuperModel extends Backbone.Model
@collections = {}
@schemas = {}
populateModel: (model) ->
populateModel: (model, resName) ->
@mustPopulate = model
model.saveBackups = @shouldSaveBackups(model)
# model.fetch() unless model.loaded or model.loading
# @listenToOnce(model, 'sync', @modelLoaded) unless model.loaded
# @listenToOnce(model, 'error', @modelErrored) unless model.loaded
url = model.url()
@models[url] = model unless @models[url]?
@modelLoaded(model) if model.loaded
resName = url unless resName
modelRes = @addModelResource(model, url)
schema = model.schema()
schemaRes = @addModelResource(schema, schema.urlRoot)
@ -87,7 +86,7 @@ module.exports = class SuperModel extends Backbone.Model
collection
finished: ->
return ResourceManager.progress == 1.0 or Object.keys(ResourceManager.resources).length == 0
return ResourceManager.progress is 1.0 or Object.keys(ResourceManager.resources).length is 0
addModelResource: (modelOrCollection, name, fetchOptions, value=1)->
@ -127,7 +126,7 @@ module.exports = class SuperModel extends Backbone.Model
onResourceLoaded: (r)=>
@modelLoaded(r.model)
# Check if the model has references
if r.constructor.name == 'ModelResource'
if r.constructor.name is 'ModelResource'
model = r.model
@addModelRefencesToLoad(model)
@updateProgress(r)
@ -189,7 +188,7 @@ class Resource extends Backbone.Model
addDependency: (name)->
depRes = ResourceManager.resources[name]
throw new Error('Resource not found') unless depRes
return if (depRes.isLoaded or name == @name)
return if (depRes.isLoaded or name is @name)
@dependencies.push(name)
markLoaded: ()->
@ -236,12 +235,12 @@ class ModelResource extends Resource
onLoadDependenciesSuccess: ()=>
@model.fetch(@fetchOptions)
@listenToOnce(@model, 'sync', ()=>
@listenToOnce(@model, 'sync', ->
@markLoaded()
@loadDeferred.resolve(@)
)
@listenToOnce(@model, 'error', ()=>
@listenToOnce(@model, 'error', ->
@markFailed()
@loadDeferred.reject(@)
)

View file

@ -50,7 +50,7 @@ module.exports = class EditorLevelView extends View
@hideLoading()
@insertSubView(new ErrorView())
)
@supermodel.populateModel @level
@supermodel.populateModel(@level, 'level')
showLoading: ($el) ->
$el ?= @$el.find('.tab-content')

View file

@ -73,7 +73,8 @@ module.exports = class ThangsTabView extends View
onThangTypesLoaded: ->
return if @destroyed
@supermodel.addCollection @thangTypes
@supermodel.populateModel model for model in @thangTypes.models
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
@ -402,7 +403,7 @@ module.exports = class ThangsTabView extends View
physical.config.pos = x: pos.x, y: pos.y, z: physical.config.pos.z if physical
thang = thangType: thangType.get('original'), id: thangID, components: components
@thangsTreema.insert '', thang
@supermodel.populateModel thangType # Make sure we grab any new data for the thang we just added
@supermodel.populateModel(thangType, thangType.get('name')) # Make sure we grab any new data for the thang we just added
editThang: (e) ->
if e.target # click event