Fixed patches denormalization.

This commit is contained in:
Scott Erickson 2014-04-17 15:44:19 -07:00
parent 7ccaa8c843
commit 21e8d7b26f
6 changed files with 15 additions and 13 deletions

View file

@ -68,8 +68,6 @@ class CocoModel extends Backbone.Model
@markToRevert()
@clearBackup()
@trigger "save", @
PatchModel = require 'models/Patch'
PatchModel.setStatus id, 'accepted' for id in @get('acceptedPatches') or []
return super attrs, options
fetch: ->
@ -221,12 +219,6 @@ class CocoModel extends Backbone.Model
delta = @getDelta()
deltasLib.expandDelta(delta, @_revertAttributes, @schema())
addPatchToAcceptOnSave: (patch) ->
acceptedPatches = @get('acceptedPatches') or [] # not actually stored on the db
acceptedPatches.push patch.id
acceptedPatches = _.uniq(acceptedPatches)
@set 'acceptedPatches', acceptedPatches
watch: (doWatch=true) ->
$.ajax("#{@urlRoot}/#{@id}/watch", {type:'PUT', data:{on:doWatch}})
@watching = -> doWatch

View file

@ -101,7 +101,7 @@ module.exports = class EditorLevelView extends View
Backbone.Mediator.publish 'level-loaded', level: @level
@showReadOnly() if me.get('anonymous')
@patchesView = @insertSubView(new PatchesView(@level), @$el.find('.patches-view'))
@listenTo @patchesView, 'accepted-patch', -> location.reload()
@listenTo @patchesView, 'accepted-patch', -> setTimeout "location.reload()", 400
@$el.find('#level-watch-button').find('> span').toggleClass('secret') if @level.watching()
onPlayLevel: (e) ->

View file

@ -52,7 +52,7 @@ module.exports = class PatchModal extends ModalView
acceptPatch: ->
delta = @deltaView.getApplicableDelta()
@targetModel.applyDelta(delta)
@targetModel.addPatchToAcceptOnSave(@patch)
@patch.setStatus('accepted')
@trigger 'accepted-patch'
@hide()

View file

@ -44,8 +44,11 @@ module.exports = class PatchesView extends CocoView
@$el.find(".#{@status}").addClass 'active'
onStatusButtonsChanged: (e) ->
@loaded = false
@status = $(e.target).val()
@reloadPatches()
reloadPatches: ->
@loaded = false
@initPatches()
@load()
@render()
@ -54,4 +57,8 @@ module.exports = class PatchesView extends CocoView
patch = _.find @patches.models, {id:$(e.target).data('patch-id')}
modal = new PatchModal(patch, @model)
@openModalView(modal)
@listenTo modal, 'accepted-patch', -> @trigger 'accepted-patch'
@listenTo modal, 'accepted-patch', -> @trigger 'accepted-patch'
@listenTo modal, 'hide', ->
f = => @reloadPatches()
setTimeout(f, 400)
@stopListening modal

View file

@ -45,9 +45,11 @@ module.exports = class ModalView extends CocoView
super($el)
hide: ->
@trigger 'hide'
@$el.removeClass('fade').modal "hide"
onHidden: ->
@trigger 'hidden'
destroy: ->
@hide() unless @hidden

View file

@ -39,8 +39,9 @@ PatchSchema.pre 'save', (next) ->
target.original = targetID
patches = document.get('patches') or []
patches = _.clone patches
patches.push @_id
document.set 'patches', patches
document.set 'patches', patches, {strict: false}
document.save (err) -> next(err)
module.exports = mongoose.model('patch', PatchSchema)