a super-hack to trick derby into showing notifications. bug fix on

refresh disappearing habits `model.get(path, {getRef:false})`
This commit is contained in:
Tyler Renelle 2013-02-04 02:36:11 -05:00
parent dfc63695c7
commit 6e2035eef6
2 changed files with 17 additions and 5 deletions

View file

@ -83,7 +83,7 @@ module.exports.BatchUpdate = BatchUpdate = (model) ->
# Additionally, for some reason after getting the user object, changing properies manually (userObj.stats.hp = 50)
# seems to actually run user.set('stats.hp',50) which we don't want to do - so we deepClone here
#_.each Object.keys(userSchema), (key) -> obj[key] = lodash.cloneDeep user.get(key)
obj = user.get()
obj = model.get('users.'+user.get('id'), {getRef:false})
###
Handles updating the user model. If this is an en-mass operation (eg, server cron), changes are queued
@ -98,9 +98,10 @@ module.exports.BatchUpdate = BatchUpdate = (model) ->
Hack to get around dom bindings being lost if parent objects are replaced whole-sale
eg, user.set('stats', {hp:50, exp:10...}) will break dom bindings, but user.set('stats.hp',50) is ok
###
setStats: ->
setStats: (stats) ->
stats ?= obj.stats
that = @
_.each Object.keys(obj.stats), (key) -> that.set "stats.#{key}", obj.stats[key]
_.each Object.keys(stats), (key) -> that.set "stats.#{key}", stats[key]
# queue: (path, val) ->
# # Special function for setting object properties by string dot-notation. See http://stackoverflow.com/a/6394168/362790

View file

@ -108,6 +108,7 @@ score = (taskId, direction, times, batch, cron) ->
{money, hp, exp, lvl} = obj.stats
taskPath = "tasks.#{taskId}"
taskObj = obj.tasks[taskId]
{type, value} = taskObj
@ -141,7 +142,10 @@ score = (taskId, direction, times, batch, cron) ->
historyEntry = { date: +new Date, value: value }
if (delta > 0) then addPoints() else subtractPoints()
taskObj.history ?= []
taskObj.history.push historyEntry if taskObj.value != value
if taskObj.value != value
taskObj.history.push historyEntry
batch.set "#{taskPath}.history", taskObj.history
when 'daily'
calculateDelta()
@ -150,6 +154,8 @@ score = (taskId, direction, times, batch, cron) ->
taskObj.history ?= []
taskObj.history.push { date: +new Date, value: value }
taskObj.completed = false
batch.set "#{taskPath}.history", taskObj.history
batchSet "#{taskObj}.completed", false
else
addPoints() # obviously for delta>0, but also a trick to undo accidental checkboxes
@ -170,9 +176,14 @@ score = (taskId, direction, times, batch, cron) ->
money = 0
taskObj.value = value
batch.set "tasks.#{taskId}", taskObj
batch.set "#{taskPath}.value", taskObj.value
origStats = _.clone obj.stats
updateStats {hp: hp, exp: exp, money: money}, batch
if commit
# newStats / origStats is a glorious hack to trick Derby into seeing the change in model.on(*)
newStats = _.clone batch.obj().stats
_.each Object.keys(origStats), (key) -> obj.stats[key] = origStats[key]
batch.setStats(newStats)
batch.setStats()
batch.commit()
return delta