From becad06750279f5f5097b4be389a163f077b9f9c Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Thu, 18 Feb 2016 16:41:06 -0800 Subject: [PATCH] Refactor ArticleEditView and PatchesView to use model endpoint functions --- app/collections/Patches.coffee | 5 +++++ app/models/CocoModel.coffee | 22 ++++++++++++++++++- app/models/SuperModel.coffee | 14 +++++++++++- .../editor/modal/save-version-modal.jade | 2 +- app/views/editor/PatchesView.coffee | 6 +++-- .../editor/article/ArticleEditView.coffee | 11 +++++----- server/middleware/patchable.coffee | 1 + spec/server/common.coffee | 2 +- 8 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 app/collections/Patches.coffee diff --git a/app/collections/Patches.coffee b/app/collections/Patches.coffee new file mode 100644 index 000000000..f9b33751f --- /dev/null +++ b/app/collections/Patches.coffee @@ -0,0 +1,5 @@ +PatchModel = require 'models/Patch' +CocoCollection = require 'collections/CocoCollection' + +module.exports = class Patches extends CocoCollection + model: PatchModel diff --git a/app/models/CocoModel.coffee b/app/models/CocoModel.coffee index 6d35f2dc8..6ec3c1ef6 100644 --- a/app/models/CocoModel.coffee +++ b/app/models/CocoModel.coffee @@ -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 diff --git a/app/models/SuperModel.coffee b/app/models/SuperModel.coffee index 3da8f37e9..39b44b579 100644 --- a/app/models/SuperModel.coffee +++ b/app/models/SuperModel.coffee @@ -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() diff --git a/app/templates/editor/modal/save-version-modal.jade b/app/templates/editor/modal/save-version-modal.jade index 3014936c0..fabc5e88f 100644 --- a/app/templates/editor/modal/save-version-modal.jade +++ b/app/templates/editor/modal/save-version-modal.jade @@ -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") diff --git a/app/views/editor/PatchesView.coffee b/app/views/editor/PatchesView.coffee index feb291197..159e76904 100644 --- a/app/views/editor/PatchesView.coffee +++ b/app/views/editor/PatchesView.coffee @@ -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() diff --git a/app/views/editor/article/ArticleEditView.coffee b/app/views/editor/article/ArticleEditView.coffee index b57b9af81..cd9a638ac 100644 --- a/app/views/editor/article/ArticleEditView.coffee +++ b/app/views/editor/article/ArticleEditView.coffee @@ -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) -> diff --git a/server/middleware/patchable.coffee b/server/middleware/patchable.coffee index 083b785f3..ee76f4346 100644 --- a/server/middleware/patchable.coffee +++ b/server/middleware/patchable.coffee @@ -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) -> diff --git a/spec/server/common.coffee b/spec/server/common.coffee index 7e75afe3c..64e715b8e 100644 --- a/spec/server/common.coffee +++ b/spec/server/common.coffee @@ -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