Patch modal handles invalid patches.

This commit is contained in:
Scott Erickson 2014-06-24 17:25:01 -04:00
parent 2cdd7c4115
commit 9b873cfab4
4 changed files with 25 additions and 17 deletions

View file

@ -18,6 +18,6 @@ class NameLoader extends CocoClass
loadedNames: (newNames) => loadedNames: (newNames) =>
_.extend namesCache, newNames _.extend namesCache, newNames
getName: (id) -> namesCache[id].name getName: (id) -> namesCache[id]?.name or id
module.exports = new NameLoader() module.exports = new NameLoader()

View file

@ -211,7 +211,9 @@ class CocoModel extends Backbone.Model
jsondiffpatch.patch newAttributes, delta jsondiffpatch.patch newAttributes, delta
catch error catch error
console.error "Error applying delta", delta, "to attributes", newAttributes, error console.error "Error applying delta", delta, "to attributes", newAttributes, error
return false
@set newAttributes @set newAttributes
return true
getExpandedDelta: -> getExpandedDelta: ->
delta = @getDelta() delta = @getDelta()

View file

@ -7,7 +7,10 @@ block modal-header-content
block modal-body-content block modal-body-content
.modal-body .modal-body
p= patch.get('commitMessage') p= patch.get('commitMessage')
if deltaWorked
.changes-stub .changes-stub
else
.alert.alert-danger Could not apply this delta to the current version.
block modal-footer block modal-footer

View file

@ -8,7 +8,7 @@ module.exports = class PatchModal extends ModalView
template: template template: template
plain: true plain: true
modalWidthPercent: 60 modalWidthPercent: 60
@DOC_SKIP_PATHS = ['_id','version', 'commitMessage', 'parent', 'created', 'slug', 'index', '__v', 'patches'] @DOC_SKIP_PATHS = ['_id','version', 'commitMessage', 'parent', 'created', 'slug', 'index', '__v', 'patches', 'creator']
events: events:
'click #withdraw-button': 'withdrawPatch' 'click #withdraw-button': 'withdrawPatch'
@ -24,29 +24,32 @@ module.exports = class PatchModal extends ModalView
@originalSource = new @targetModel.constructor({_id:targetID}) @originalSource = new @targetModel.constructor({_id:targetID})
@supermodel.loadModel @originalSource, 'source_document' @supermodel.loadModel @originalSource, 'source_document'
onLoaded: ->
@headModel = null
if @targetModel.hasWriteAccess()
@headModel = @originalSource.clone(false)
@headModel.markToRevert true
@headModel.set(@targetModel.attributes)
@headModel.loaded = true
@pendingModel = @originalSource.clone(false)
@pendingModel.markToRevert true
@deltaWorked = @pendingModel.applyDelta(@patch.get('delta'))
@pendingModel.loaded = true
super()
getRenderData: -> getRenderData: ->
c = super() c = super()
c.isPatchCreator = @patch.get('creator') is auth.me.id c.isPatchCreator = @patch.get('creator') is auth.me.id
c.isPatchRecipient = @targetModel.hasWriteAccess() c.isPatchRecipient = @targetModel.hasWriteAccess()
c.status = @patch.get 'status' c.status = @patch.get 'status'
c.patch = @patch c.patch = @patch
c.deltaWorked = @deltaWorked
c c
afterRender: -> afterRender: ->
return unless @supermodel.finished() return super() unless @supermodel.finished() and @deltaWorked
headModel = null @deltaView = new DeltaView({model:@pendingModel, headModel:@headModel, skipPaths: PatchModal.DOC_SKIP_PATHS})
if @targetModel.hasWriteAccess()
headModel = @originalSource.clone(false)
headModel.markToRevert true
headModel.set(@targetModel.attributes)
headModel.loaded = true
pendingModel = @originalSource.clone(false)
pendingModel.markToRevert true
pendingModel.applyDelta(@patch.get('delta'))
pendingModel.loaded = true
@deltaView = new DeltaView({model:pendingModel, headModel:headModel, skipPaths: PatchModal.DOC_SKIP_PATHS})
changeEl = @$el.find('.changes-stub') changeEl = @$el.find('.changes-stub')
@insertSubView(@deltaView, changeEl) @insertSubView(@deltaView, changeEl)
super() super()