Refactor ArticleEditView and PatchesView to use model endpoint functions

This commit is contained in:
Scott Erickson 2016-02-18 16:41:06 -08:00
parent 7fb08f343a
commit becad06750
8 changed files with 51 additions and 12 deletions

View file

@ -0,0 +1,5 @@
PatchModel = require 'models/Patch'
CocoCollection = require 'collections/CocoCollection'
module.exports = class Patches extends CocoCollection
model: PatchModel

View file

@ -426,6 +426,26 @@ class CocoModel extends Backbone.Model
# language codes that are covered for every i18n object are fully covered
overallCoverage = _.intersection(langCodeArrays...)
@set('i18nCoverage', overallCoverage)
saveNewMinorVersion: (attrs, options={}) ->
options.url = @url() + '/new-version'
options.type = 'POST'
return @save(attrs, options)
saveNewMajorVersion: (attrs, options={}) ->
attrs = attrs or _.omit(@attributes, 'version')
options.url = @url() + '/new-version'
options.type = 'POST'
options.patch = true # do not let version get sent along
return @save(attrs, options)
fetchPatchesWithStatus: (status='pending', options={}) ->
Patches = require '../collections/Patches'
patches = new Patches()
options.data ?= {}
options.data.status = status
options.url = @urlRoot + '/' + @get('original') + '/patches'
patches.fetch(options)
return patches
module.exports = CocoModel

View file

@ -80,7 +80,16 @@ module.exports = class SuperModel extends Backbone.Model
res = @addModelResource(collection, name, fetchOptions, value)
res.load() if not (res.isLoading or res.isLoaded)
return res
# Eventually should use only these functions. Use SuperModel just to track progress.
trackModel: (model, value) ->
res = @addModelResource(collection, '', {}, value)
res.listen()
trackCollection: (collection, value) ->
res = @addModelResource(collection, '', {}, value)
res.listen()
# replace or overwrite
shouldSaveBackups: (model) -> false
@ -275,6 +284,9 @@ class ModelResource extends Resource
fetchModel: ->
@jqxhr = @model.fetch(@fetchOptions) unless @model.loading
@listen()
listen: ->
@listenToOnce @model, 'sync', -> @markLoaded()
@listenToOnce @model, 'error', -> @markFailed()

View file

@ -12,7 +12,7 @@ block modal-body-content
form.form-inline
.form-group.commit-message
input.form-control#commit-message(name="commitMessage", type="text")
if !view.isPatch
if !view.isPatch && !view.options.noNewMajorVersions
.checkbox
label
input#major-version(name="version-is-major", type="checkbox")

View file

@ -19,11 +19,12 @@ module.exports = class PatchesView extends CocoView
initPatches: ->
@startedLoading = false
@patches = new PatchesCollection([], {}, @model, @status)
@patches = @model.fetchPatchesWithStatus()
load: ->
@initPatches()
@patches = @supermodel.loadCollection(@patches, 'patches', {cache: false}).model
@patches = @model.fetchPatchesWithStatus(@status, {cache: false})
@supermodel.trackCollection(@patches)
@listenTo @patches, 'sync', @onPatchesLoaded
onPatchesLoaded: ->
@ -40,6 +41,7 @@ module.exports = class PatchesView extends CocoView
afterRender: ->
@$el.find(".#{@status}").addClass 'active'
super()
onStatusButtonsChanged: (e) ->
@status = $(e.target).val()

View file

@ -18,7 +18,7 @@ module.exports = class ArticleEditView extends RootView
constructor: (options, @articleID) ->
super options
@article = new Article(_id: @articleID)
@article = new Article({_id: @articleID})
@article.saveBackups = true
@supermodel.loadModel @article, 'article'
@pushChangesToPreview = _.throttle(@pushChangesToPreview, 500)
@ -73,7 +73,7 @@ module.exports = class ArticleEditView extends RootView
return false
openSaveModal: ->
modal = new SaveVersionModal({model: @article})
modal = new SaveVersionModal({model: @article, noNewMajorVersions: true})
@openModalView(modal)
@listenToOnce modal, 'save-new-version', @saveNewArticle
@listenToOnce modal, 'hidden', -> @stopListening(modal)
@ -83,9 +83,8 @@ module.exports = class ArticleEditView extends RootView
for key, value of @treema.data
@article.set(key, value)
newArticle = if e.major then @article.cloneNewMajorVersion() else @article.cloneNewMinorVersion()
newArticle.set('commitMessage', e.commitMessage)
res = newArticle.save(null, {type: 'POST'}) # Override PUT so we can trigger postNewVersion logic
@article.set('commitMessage', e.commitMessage)
res = @article.saveNewMinorVersion()
return unless res
modal = @$el.find('#save-version-modal')
@enableModalInProgress(modal)
@ -96,7 +95,7 @@ module.exports = class ArticleEditView extends RootView
res.success =>
@article.clearBackup()
modal.modal('hide')
url = "/editor/article/#{newArticle.get('slug') or newArticle.id}"
url = "/editor/article/#{@article.get('slug') or @article.id}"
document.location.href = url
showVersionHistory: (e) ->

View file

@ -3,6 +3,7 @@ errors = require '../commons/errors'
wrap = require 'co-express'
Promise = require 'bluebird'
Patch = require '../models/Patch'
mongoose = require 'mongoose'
module.exports =
patches: (options={}) -> wrap (req, res) ->

View file

@ -8,7 +8,7 @@ if process.env.COCO_MONGO_HOST
GLOBAL._ = require 'lodash'
_.str = require 'underscore.string'
_.mixin(_.str.exports())
GLOBAL.mongoose = require 'mongoose'
GLOBAL.mongoose = require 'mongoose' # TODO: Remove, otherwise it hides when the server is missing a mongoose require
path = require 'path'
GLOBAL.testing = true
GLOBAL.tv4 = require 'tv4' # required for TreemaUtils to work