Merge pull request #794 from gintau/feature/loading-views

Refactor CocoView
This commit is contained in:
Scott Erickson 2014-04-07 14:55:14 -07:00
commit 5c6a0bc252
2 changed files with 116 additions and 105 deletions

160
.gitignore vendored
View file

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