Refactor ArticleEditView and PatchesView to use model endpoint functions
This commit is contained in:
parent
7fb08f343a
commit
becad06750
8 changed files with 51 additions and 12 deletions
app
collections
models
templates/editor/modal
views/editor
server/middleware
spec/server
5
app/collections/Patches.coffee
Normal file
5
app/collections/Patches.coffee
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
PatchModel = require 'models/Patch'
|
||||||
|
CocoCollection = require 'collections/CocoCollection'
|
||||||
|
|
||||||
|
module.exports = class Patches extends CocoCollection
|
||||||
|
model: PatchModel
|
|
@ -426,6 +426,26 @@ class CocoModel extends Backbone.Model
|
||||||
# language codes that are covered for every i18n object are fully covered
|
# language codes that are covered for every i18n object are fully covered
|
||||||
overallCoverage = _.intersection(langCodeArrays...)
|
overallCoverage = _.intersection(langCodeArrays...)
|
||||||
@set('i18nCoverage', overallCoverage)
|
@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
|
module.exports = CocoModel
|
||||||
|
|
|
@ -80,7 +80,16 @@ module.exports = class SuperModel extends Backbone.Model
|
||||||
res = @addModelResource(collection, name, fetchOptions, value)
|
res = @addModelResource(collection, name, fetchOptions, value)
|
||||||
res.load() if not (res.isLoading or res.isLoaded)
|
res.load() if not (res.isLoading or res.isLoaded)
|
||||||
return res
|
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
|
# replace or overwrite
|
||||||
shouldSaveBackups: (model) -> false
|
shouldSaveBackups: (model) -> false
|
||||||
|
|
||||||
|
@ -275,6 +284,9 @@ class ModelResource extends Resource
|
||||||
|
|
||||||
fetchModel: ->
|
fetchModel: ->
|
||||||
@jqxhr = @model.fetch(@fetchOptions) unless @model.loading
|
@jqxhr = @model.fetch(@fetchOptions) unless @model.loading
|
||||||
|
@listen()
|
||||||
|
|
||||||
|
listen: ->
|
||||||
@listenToOnce @model, 'sync', -> @markLoaded()
|
@listenToOnce @model, 'sync', -> @markLoaded()
|
||||||
@listenToOnce @model, 'error', -> @markFailed()
|
@listenToOnce @model, 'error', -> @markFailed()
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ block modal-body-content
|
||||||
form.form-inline
|
form.form-inline
|
||||||
.form-group.commit-message
|
.form-group.commit-message
|
||||||
input.form-control#commit-message(name="commitMessage", type="text")
|
input.form-control#commit-message(name="commitMessage", type="text")
|
||||||
if !view.isPatch
|
if !view.isPatch && !view.options.noNewMajorVersions
|
||||||
.checkbox
|
.checkbox
|
||||||
label
|
label
|
||||||
input#major-version(name="version-is-major", type="checkbox")
|
input#major-version(name="version-is-major", type="checkbox")
|
||||||
|
|
|
@ -19,11 +19,12 @@ module.exports = class PatchesView extends CocoView
|
||||||
|
|
||||||
initPatches: ->
|
initPatches: ->
|
||||||
@startedLoading = false
|
@startedLoading = false
|
||||||
@patches = new PatchesCollection([], {}, @model, @status)
|
@patches = @model.fetchPatchesWithStatus()
|
||||||
|
|
||||||
load: ->
|
load: ->
|
||||||
@initPatches()
|
@initPatches()
|
||||||
@patches = @supermodel.loadCollection(@patches, 'patches', {cache: false}).model
|
@patches = @model.fetchPatchesWithStatus(@status, {cache: false})
|
||||||
|
@supermodel.trackCollection(@patches)
|
||||||
@listenTo @patches, 'sync', @onPatchesLoaded
|
@listenTo @patches, 'sync', @onPatchesLoaded
|
||||||
|
|
||||||
onPatchesLoaded: ->
|
onPatchesLoaded: ->
|
||||||
|
@ -40,6 +41,7 @@ module.exports = class PatchesView extends CocoView
|
||||||
|
|
||||||
afterRender: ->
|
afterRender: ->
|
||||||
@$el.find(".#{@status}").addClass 'active'
|
@$el.find(".#{@status}").addClass 'active'
|
||||||
|
super()
|
||||||
|
|
||||||
onStatusButtonsChanged: (e) ->
|
onStatusButtonsChanged: (e) ->
|
||||||
@status = $(e.target).val()
|
@status = $(e.target).val()
|
||||||
|
|
|
@ -18,7 +18,7 @@ module.exports = class ArticleEditView extends RootView
|
||||||
|
|
||||||
constructor: (options, @articleID) ->
|
constructor: (options, @articleID) ->
|
||||||
super options
|
super options
|
||||||
@article = new Article(_id: @articleID)
|
@article = new Article({_id: @articleID})
|
||||||
@article.saveBackups = true
|
@article.saveBackups = true
|
||||||
@supermodel.loadModel @article, 'article'
|
@supermodel.loadModel @article, 'article'
|
||||||
@pushChangesToPreview = _.throttle(@pushChangesToPreview, 500)
|
@pushChangesToPreview = _.throttle(@pushChangesToPreview, 500)
|
||||||
|
@ -73,7 +73,7 @@ module.exports = class ArticleEditView extends RootView
|
||||||
return false
|
return false
|
||||||
|
|
||||||
openSaveModal: ->
|
openSaveModal: ->
|
||||||
modal = new SaveVersionModal({model: @article})
|
modal = new SaveVersionModal({model: @article, noNewMajorVersions: true})
|
||||||
@openModalView(modal)
|
@openModalView(modal)
|
||||||
@listenToOnce modal, 'save-new-version', @saveNewArticle
|
@listenToOnce modal, 'save-new-version', @saveNewArticle
|
||||||
@listenToOnce modal, 'hidden', -> @stopListening(modal)
|
@listenToOnce modal, 'hidden', -> @stopListening(modal)
|
||||||
|
@ -83,9 +83,8 @@ module.exports = class ArticleEditView extends RootView
|
||||||
for key, value of @treema.data
|
for key, value of @treema.data
|
||||||
@article.set(key, value)
|
@article.set(key, value)
|
||||||
|
|
||||||
newArticle = if e.major then @article.cloneNewMajorVersion() else @article.cloneNewMinorVersion()
|
@article.set('commitMessage', e.commitMessage)
|
||||||
newArticle.set('commitMessage', e.commitMessage)
|
res = @article.saveNewMinorVersion()
|
||||||
res = newArticle.save(null, {type: 'POST'}) # Override PUT so we can trigger postNewVersion logic
|
|
||||||
return unless res
|
return unless res
|
||||||
modal = @$el.find('#save-version-modal')
|
modal = @$el.find('#save-version-modal')
|
||||||
@enableModalInProgress(modal)
|
@enableModalInProgress(modal)
|
||||||
|
@ -96,7 +95,7 @@ module.exports = class ArticleEditView extends RootView
|
||||||
res.success =>
|
res.success =>
|
||||||
@article.clearBackup()
|
@article.clearBackup()
|
||||||
modal.modal('hide')
|
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
|
document.location.href = url
|
||||||
|
|
||||||
showVersionHistory: (e) ->
|
showVersionHistory: (e) ->
|
||||||
|
|
|
@ -3,6 +3,7 @@ errors = require '../commons/errors'
|
||||||
wrap = require 'co-express'
|
wrap = require 'co-express'
|
||||||
Promise = require 'bluebird'
|
Promise = require 'bluebird'
|
||||||
Patch = require '../models/Patch'
|
Patch = require '../models/Patch'
|
||||||
|
mongoose = require 'mongoose'
|
||||||
|
|
||||||
module.exports =
|
module.exports =
|
||||||
patches: (options={}) -> wrap (req, res) ->
|
patches: (options={}) -> wrap (req, res) ->
|
||||||
|
|
|
@ -8,7 +8,7 @@ if process.env.COCO_MONGO_HOST
|
||||||
GLOBAL._ = require 'lodash'
|
GLOBAL._ = require 'lodash'
|
||||||
_.str = require 'underscore.string'
|
_.str = require 'underscore.string'
|
||||||
_.mixin(_.str.exports())
|
_.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'
|
path = require 'path'
|
||||||
GLOBAL.testing = true
|
GLOBAL.testing = true
|
||||||
GLOBAL.tv4 = require 'tv4' # required for TreemaUtils to work
|
GLOBAL.tv4 = require 'tv4' # required for TreemaUtils to work
|
||||||
|
|
Reference in a new issue