fix(draft-store-proxy): Centralize check for body

This commit is contained in:
Ben Gotow 2015-03-26 18:28:11 -07:00
parent 7e6d5b64d8
commit e5f3f3087b

View file

@ -85,10 +85,7 @@ class DraftStoreProxy
@_draftPromise ?= new Promise (resolve, reject) => @_draftPromise ?= new Promise (resolve, reject) =>
DatabaseStore = require './database-store' DatabaseStore = require './database-store'
DatabaseStore.findByLocalId(Message, @draftLocalId).then (draft) => DatabaseStore.findByLocalId(Message, @draftLocalId).then (draft) =>
if !draft.body? @_setDraft(draft)
throw new Error("DraftStoreProxy.prepare - draft has no body.")
@_draft = draft
@_emitter.emit('trigger')
resolve(@) resolve(@)
.catch(reject) .catch(reject)
@_draftPromise @_draftPromise
@ -107,6 +104,12 @@ class DraftStoreProxy
# Unlink ourselves from the stores/actions we were listening to # Unlink ourselves from the stores/actions we were listening to
# so that we can be garbage collected # so that we can be garbage collected
unlisten() for unlisten in @unlisteners unlisten() for unlisten in @unlisteners
_setDraft: (draft) ->
if !draft.body?
throw new Error("DraftStoreProxy._setDraft - new draft has no body!")
@_draft = draft
@_emitter.emit('trigger')
_onDraftChanged: (change) -> _onDraftChanged: (change) ->
return if not change? return if not change?
@ -116,13 +119,11 @@ class DraftStoreProxy
# Is this change an update to our draft? # Is this change an update to our draft?
myDraft = _.find(change.objects, (obj) => obj.id == @_draft.id) myDraft = _.find(change.objects, (obj) => obj.id == @_draft.id)
if myDraft if myDraft
@_draft = myDraft @_setDraft(myDraft)
@_emitter.emit('trigger')
_onDraftSwapped: (change) -> _onDraftSwapped: (change) ->
# A draft was saved with a new ID. Since we use the draft ID to # A draft was saved with a new ID. Since we use the draft ID to
# watch for changes to our draft, we need to pull again using our # watch for changes to our draft, we need to pull again using our
# localId. # localId.
if change.oldModel.id is @_draft.id if change.oldModel.id is @_draft.id
@_draft = change.newModel @_setDraft(change.newModel)
@_emitter.emit('trigger')