Refactor CocoView.

This commit is contained in:
Ting-Kuan 2014-04-06 16:37:00 -04:00
parent 3feb694756
commit 20c624e0b8
2 changed files with 39 additions and 105 deletions

80
.gitignore vendored
View file

@ -1,80 +0,0 @@
### If you add something here, copy it to the end of .npmignore, too. ###
# Python
*.pyc
*.pyo
# OS X
.DS_Store
Icon?
._*
.Spotlight-V100
.Trashes
# Windows
Thumbs.db
# Emacs
*.*~
*.#
.#*
*#
# Vim
.*.sw[a-z]
*.un~i
# Sublime
*.sublime-project
*.sublime-workspace
# NPM packages folder.
node_modules/
bower_components/
# Some other random stuff
always-ignore extensions
*.diff
*.err
*.orig
*.log
*.rej
*.vi
*.sass-cache
# OS or Editor folders
.cache
.project
.settings
.tmproj
.idea
nbproject
# Brunch folder for temporary files.
tmp/
# Brunch output
public/
# Redis?
dump.rdb
# Mongo
mongo/
# Karma coverage
coverage/
# Floo
.floo
FLOOBITS_README.md
# mongodb
db/
bin/node/
bin/mongo/
# windows
/SCOCODE.bat
### If you add something here, copy it to the end of .npmignore, too. ###

View file

@ -11,7 +11,6 @@ makeScopeName = -> "view-scope-#{classCount++}"
doNothing = -> doNothing = ->
module.exports = class CocoView extends Backbone.View module.exports = class CocoView extends Backbone.View
startsLoading: false
cache: false # signals to the router to keep this view around cache: false # signals to the router to keep this view around
template: -> '' template: -> ''
@ -89,7 +88,7 @@ module.exports = class CocoView extends Backbone.View
return @template if _.isString(@template) return @template if _.isString(@template)
@$el.html @template(@getRenderData()) @$el.html @template(@getRenderData())
@afterRender() @afterRender()
@showLoading() if @startsLoading or @loading() # TODO: Remove startsLoading entirely @showLoading() if @loading()
@$el.i18n() @$el.i18n()
@ @
@ -108,25 +107,45 @@ module.exports = class CocoView extends Backbone.View
# Resource and request loading management for any given view # Resource and request loading management for any given view
addResourceToLoad: (modelOrCollection, name, value=1) -> addResourceToLoad: (modelOrCollection, name, value=1) ->
@loadProgress.resources.push {resource:modelOrCollection, value:value, name:name} res = {resource:modelOrCollection, value:value, name:name, loaded: modelOrCollection.loaded}
@listenToOnce modelOrCollection, 'sync', @updateProgress
@loadProgress.resources.push res
@loadProgress.denom += value
@listenToOnce modelOrCollection, 'sync', ()=>
# Sprite builder only works after rendering, if callback creates sprite, we need to update progress first.
res.loaded = true
@updateProgress(res)
@listenTo modelOrCollection, 'error', @onResourceLoadFailed @listenTo modelOrCollection, 'error', @onResourceLoadFailed
@updateProgress() @updateProgress(res)
addRequestToLoad: (jqxhr, name, retryFunc, value=1) -> addRequestToLoad: (jqxhr, name, retryFunc, value=1) ->
@loadProgress.requests.push {request:jqxhr, value:value, name: name, retryFunc: retryFunc} res = {request:jqxhr, value:value, name: name, retryFunc: retryFunc, loaded:false}
jqxhr.done @updateProgress
jqxhr.fail @onRequestLoadFailed @loadProgress.requests.push res
@loadProgress.denom += value
jqxhr.done ()=>
res.loaded = true
@updateProgress(res)
jqxhr.fail ()=>
@onRequestLoadFailed(jqxhr)
addSomethingToLoad: (name, value=1) -> addSomethingToLoad: (name, value=1) ->
@loadProgress.somethings.push {loaded: false, name: name, value: value} res = {name: name, value: value, loaded: false}
@updateProgress()
@loadProgress.somethings.push res
@loadProgress.denom += value
@updateProgress(res)
somethingLoaded: (name) -> somethingLoaded: (name) ->
r = _.find @loadProgress.somethings, {name: name} r = _.find @loadProgress.somethings, {name: name}
return console.error 'Could not find something called', name if not r return console.error 'Could not find something called', name if not r
r.loaded = true r.loaded = true
@updateProgress(name) @updateProgress(r)
loading: -> loading: ->
return false if @loaded return false if @loaded
@ -138,27 +157,21 @@ module.exports = class CocoView extends Backbone.View
return true if not r.loaded return true if not r.loaded
return false return false
updateProgress: => updateProgress: (r)=>
console.debug 'Loaded', r.name if arguments[0] and r = _.find @loadProgress.resources, {resource:arguments[0]} console.debug 'Loaded', r.name, r.loaded
console.debug 'Loaded', r.name if arguments[2] and r = _.find @loadProgress.requests, {request:arguments[2]}
console.debug 'Loaded', r.name if arguments[0] and r = _.find @loadProgress.somethings, {name:arguments[0]}
denom = 0 denom = @loadProgress.denom
denom += r.value for r in @loadProgress.resources @loadProgress.num += r.value if r.loaded
denom += r.value for r in @loadProgress.requests
denom += r.value for r in @loadProgress.somethings
num = @loadProgress.num num = @loadProgress.num
num += r.value for r in @loadProgress.resources when r.resource.loaded
num += r.value for r in @loadProgress.requests when r.request.status
num += r.value for r in @loadProgress.somethings when r.loaded
#console.log 'update progress', @, num, denom, arguments
progress = if denom then num / denom else 0 progress = if denom then num / denom else 0
# sometimes the denominator isn't known from the outset, so make sure the overall progress only goes up # sometimes the denominator isn't known from the outset, so make sure the overall progress only goes up
@loadProgress.progress = progress if progress > @loadProgress.progress @loadProgress.progress = progress if progress > @loadProgress.progress
@updateProgressBar() @updateProgressBar()
if num is denom and not @loaded
@loaded = true console.debug 'gintau', 'updateProgress', num, denom
if num is denom
@onLoaded() @onLoaded()
updateProgressBar: => updateProgressBar: =>
@ -166,6 +179,7 @@ module.exports = class CocoView extends Backbone.View
@$el.find('.loading-screen .progress-bar').css('width', prog) @$el.find('.loading-screen .progress-bar').css('width', prog)
onLoaded: -> onLoaded: ->
console.debug 'gintau', 'CocoView-Render()', @
@render() @render()
# Error handling for loading # Error handling for loading