Fixed up my refactoring of markToRevert so now _revertAttributes is only populated the first time set is called.

This commit is contained in:
Scott Erickson 2014-05-30 16:20:20 -07:00
parent bba0e4939d
commit 47f28d26d1
2 changed files with 8 additions and 7 deletions

View file

@ -5,6 +5,7 @@ BEEN_HERE_BEFORE_KEY = 'beenHereBefore'
init = -> init = ->
module.exports.me = window.me = new User(window.userObject) # inserted into main.html module.exports.me = window.me = new User(window.userObject) # inserted into main.html
module.exports.me.onLoaded()
trackFirstArrival() trackFirstArrival()
if me and not me.get('testGroupNumber')? if me and not me.get('testGroupNumber')?
# Assign testGroupNumber to returning visitors; new ones in server/routes/auth # Assign testGroupNumber to returning visitors; new ones in server/routes/auth

View file

@ -15,10 +15,10 @@ class CocoModel extends Backbone.Model
super() super()
if not @constructor.className if not @constructor.className
console.error("#{@} needs a className set.") console.error("#{@} needs a className set.")
@markToRevert()
@addSchemaDefaults() @addSchemaDefaults()
@on 'sync', @onLoaded, @ @on 'sync', @onLoaded, @
@on 'error', @onError, @ @on 'error', @onError, @
@on 'add', @onLoaded, @
@saveBackup = _.debounce(@saveBackup, 500) @saveBackup = _.debounce(@saveBackup, 500)
type: -> type: ->
@ -38,14 +38,15 @@ class CocoModel extends Backbone.Model
@loaded = true @loaded = true
@loading = false @loading = false
@jqxhr = null @jqxhr = null
@markToRevert()
@loadFromBackup() @loadFromBackup()
getNormalizedURL: -> "#{@urlRoot}/#{@id}" getNormalizedURL: -> "#{@urlRoot}/#{@id}"
set: -> set: ->
inFlux = @loading or not @loaded
@markToRevert() unless inFlux or @_revertAttributes
res = super(arguments...) res = super(arguments...)
@saveBackup() if @saveBackups and @loaded and @hasLocalChanges() @saveBackup() if @saveBackups and (not inFlux) and @hasLocalChanges()
res res
loadFromBackup: -> loadFromBackup: ->
@ -77,7 +78,7 @@ class CocoModel extends Backbone.Model
options.success = (model, res) => options.success = (model, res) =>
@trigger "save:success", @ @trigger "save:success", @
success(@, res) if success success(@, res) if success
@markToRevert() @markToRevert() if @_revertAttributes
@clearBackup() @clearBackup()
options.error = (model, res) => options.error = (model, res) =>
error(@, res) if error error(@, res) if error
@ -93,8 +94,8 @@ class CocoModel extends Backbone.Model
@loading = true @loading = true
@jqxhr @jqxhr
markToRevert: (force=false) -> markToRevert: ->
return unless @saveBackups or force console.debug "Saving _revertAttributes for #{@constructor.className}: '#{@get('name')}'"
if @type() is 'ThangType' if @type() is 'ThangType'
@_revertAttributes = _.clone @attributes # No deep clones for these! @_revertAttributes = _.clone @attributes # No deep clones for these!
else else
@ -143,7 +144,6 @@ class CocoModel extends Backbone.Model
#console.log "setting", prop, "to", sch.default, "from sch.default" if sch.default? #console.log "setting", prop, "to", sch.default, "from sch.default" if sch.default?
@set prop, sch.default if sch.default? @set prop, sch.default if sch.default?
if @loaded if @loaded
@markToRevert()
@loadFromBackup() @loadFromBackup()
@isObjectID: (s) -> @isObjectID: (s) ->