FIX: editing a post wasn't showing error messages from the server

This commit is contained in:
Régis Hanol
2015-03-19 12:22:56 +01:00
parent b071bd3c7c
commit df3b1f6968
21 changed files with 113 additions and 117 deletions

View File

@@ -211,13 +211,13 @@ export default DiscourseController.extend({
}
}
var staged = false,
disableJumpReply = Discourse.User.currentProp('disable_jump_reply');
var promise = composer.save({
var staged = false;
const disableJumpReply = Discourse.User.currentProp('disable_jump_reply');
const promise = composer.save({
imageSizes: this.get('view').imageSizes(),
editReason: this.get("editReason")
}).then(function(opts) {
// If we replied as a new topic successfully, remove the draft.
if (self.get('replyAsNewTopicDraft')) {
self.destroyDraft();
@@ -240,21 +240,21 @@ export default DiscourseController.extend({
Discourse.URL.routeTo(opts.post.get('url'));
}
}
}, function(error) {
}).catch(function(error) {
composer.set('disableDrafts', false);
bootbox.alert(error);
});
if ( this.get('controllers.application.currentRouteName').split('.')[0] === 'topic' &&
composer.get('topic.id') === this.get('controllers.topic.model.id') ) {
if (this.get('controllers.application.currentRouteName').split('.')[0] === 'topic' &&
composer.get('topic.id') === this.get('controllers.topic.model.id')) {
staged = composer.get('stagedPost');
}
Em.run.schedule('afterRender', function() {
if (staged && !disableJumpReply) {
var postNumber = staged.get('post_number');
Discourse.URL.jumpToPost(postNumber, {skipIfOnScreen: true});
const postNumber = staged.get('post_number');
Discourse.URL.jumpToPost(postNumber, { skipIfOnScreen: true });
self.appEvents.trigger('post:highlight', postNumber);
}
});

View File

@@ -123,7 +123,6 @@ Discourse.Post = Discourse.Model.extend({
save: function(complete, error) {
var self = this;
if (!this.get('newPost')) {
// We're updating a post
return Discourse.ajax("/posts/" + (this.get('id')), {
type: 'PUT',
@@ -137,13 +136,12 @@ Discourse.Post = Discourse.Model.extend({
self.set('version', result.post.version);
if (result.category) Discourse.Site.current().updateCategory(result.category);
if (complete) complete(Discourse.Post.create(result.post));
}, function(result) {
}).catch(function(result) {
// Post failed to update
if (error) error(result);
});
} else {
// We're saving a post
var data = this.getProperties(Discourse.Composer.serializedFieldsForCreate());
data.reply_to_post_number = this.get('reply_to_post_number');
@@ -162,7 +160,7 @@ Discourse.Post = Discourse.Model.extend({
}).then(function(result) {
// Post created
if (complete) complete(Discourse.Post.create(result));
}, function(result) {
}).catch(function(result) {
// Failed to create a post
if (error) error(result);
});

View File

@@ -385,7 +385,7 @@ const Composer = Discourse.Model.extend({
},
save(opts) {
if( !this.get('cantSubmitPost') ) {
if (!this.get('cantSubmitPost')) {
return this.get('editingPost') ? this.editPost(opts) : this.createPost(opts);
}
},
@@ -409,8 +409,9 @@ const Composer = Discourse.Model.extend({
// When you edit a post
editPost(opts) {
const post = this.get('post'),
oldCooked = post.get('cooked'),
self = this;
oldCooked = post.get('cooked'),
self = this;
let promise;
// Update the title if we've changed it, otherwise consider it a
@@ -418,7 +419,6 @@ const Composer = Discourse.Model.extend({
if (this.get('title') &&
post.get('post_number') === 1 &&
this.get('topic.details.can_edit')) {
const topicProps = this.getProperties(Object.keys(_edit_topic_serializer));
promise = Discourse.Topic.update(this.get('topic'), topicProps);
} else {
@@ -431,33 +431,26 @@ const Composer = Discourse.Model.extend({
imageSizes: opts.imageSizes,
cooked: this.getCookedHtml()
});
this.set('composeState', CLOSED);
return promise.then(function() {
return post.save(function(result) {
post.updateFromPost(result);
self.clearState();
}).catch(function(error) {
const response = $.parseJSON(error.responseText);
if (response && response.errors) {
return(response.errors[0]);
} else {
return(I18n.t('generic_error'));
}
}, function (error) {
post.set('cooked', oldCooked);
self.set('composeState', OPEN);
const response = $.parseJSON(error.responseText);
throw response && response.errors ? response.errors[0] : I18n.t('generic_error');
});
});
},
serialize(serializer, dest) {
if (!dest) {
dest = {};
}
const self = this;
Object.keys(serializer).forEach(function(f) {
const val = self.get(serializer[f]);
dest = dest || {};
Object.keys(serializer).forEach(f => {
const val = this.get(serializer[f]);
if (typeof val !== 'undefined') {
Ember.set(dest, f, val);
}
@@ -468,9 +461,10 @@ const Composer = Discourse.Model.extend({
// Create a new Post
createPost(opts) {
const post = this.get('post'),
topic = this.get('topic'),
currentUser = Discourse.User.current(),
postStream = this.get('topic.postStream');
topic = this.get('topic'),
currentUser = Discourse.User.current(),
postStream = this.get('topic.postStream');
let addedToStream = false;
// Build the post object
@@ -530,10 +524,10 @@ const Composer = Discourse.Model.extend({
}
}
const composer = this;
const promise = new Ember.RSVP.Promise(function(resolve, reject) {
const composer = this,
promise = new Ember.RSVP.Promise(function(resolve, reject) {
composer.set('composeState', SAVING);
createdPost.save(function(result) {
let saving = true;

View File

@@ -316,9 +316,7 @@ class ApplicationController < ActionController::Base
# type - a machine-readable description of the error
# status - HTTP status code to return
def render_json_error(obj, opts={})
if opts.is_a? Fixnum
opts = {status: opts}
end
opts = { status: opts } if opts.is_a?(Fixnum)
render json: MultiJson.dump(create_errors_json(obj, opts[:type])), status: opts[:status] || 422
end