From e0fc7afc4165d0fa7621f0777c160b6ec88b51dc Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 27 Mar 2013 17:40:35 -0400 Subject: [PATCH] Fixes bug where progress looked weird when creating a new post. --- .../javascripts/discourse/models/composer.js | 10 ++++++- .../javascripts/discourse/models/post.js | 26 +++++++++---------- .../discourse/routes/discourse_location.js | 1 - 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/discourse/models/composer.js b/app/assets/javascripts/discourse/models/composer.js index 1484dd173aa..8a2a66c614a 100644 --- a/app/assets/javascripts/discourse/models/composer.js +++ b/app/assets/javascripts/discourse/models/composer.js @@ -337,6 +337,9 @@ Discourse.Composer = Discourse.Model.extend({ currentUser = Discourse.get('currentUser'), addedToStream = false; + // The post number we'll probably get from the server + var probablePostNumber = this.get('topic.highest_post_number') + 1; + // Build the post object var createdPost = Discourse.Post.create({ raw: this.get('reply'), @@ -345,7 +348,8 @@ Discourse.Composer = Discourse.Model.extend({ topic_id: this.get('topic.id'), reply_to_post_number: post ? post.get('post_number') : null, imageSizes: opts.imageSizes, - post_number: this.get('topic.highest_post_number') + 1, + post_number: probablePostNumber, + index: probablePostNumber, cooked: $('#wmd-preview').html(), reply_count: 0, display_username: currentUser.get('name'), @@ -373,6 +377,7 @@ Discourse.Composer = Discourse.Model.extend({ topic.set('last_posted_at', new Date()); topic.set('highest_post_number', createdPost.get('post_number')); topic.set('last_poster', Discourse.get('currentUser')); + topic.set('filtered_posts_count', topic.get('filtered_posts_count') + 1); // Set the topic view for the new post createdPost.set('topic', topic); @@ -400,6 +405,7 @@ Discourse.Composer = Discourse.Model.extend({ createdPost.save(function(result) { var addedPost = false, saving = true; + createdPost.updateFromSave(result); if (topic) { // It's no longer a new post @@ -410,6 +416,7 @@ Discourse.Composer = Discourse.Model.extend({ composer.set('composeState', CLOSED); saving = false; } + composer.set('reply', ''); composer.set('createdPost', createdPost); if (addedToStream) { @@ -422,6 +429,7 @@ Discourse.Composer = Discourse.Model.extend({ // If an error occurs if (topic) { topic.posts.removeObject(createdPost); + topic.set('filtered_posts_count', topic.get('filtered_posts_count') - 1); } promise.reject($.parseJSON(error.responseText).errors[0]); composer.set('composeState', OPEN); diff --git a/app/assets/javascripts/discourse/models/post.js b/app/assets/javascripts/discourse/models/post.js index 91c538258fd..0621a793bef 100644 --- a/app/assets/javascripts/discourse/models/post.js +++ b/app/assets/javascripts/discourse/models/post.js @@ -182,33 +182,31 @@ Discourse.Post = Discourse.Model.extend({ // Update the properties of this post from an obj, ignoring cooked as we should already // have that rendered. updateFromSave: function(obj) { - var lookup, - _this = this; - if (!obj) { - return; - } + + var post = this; + + // Update all the properties + if (!obj) return; Object.each(obj, function(key, val) { - if (key === 'actions_summary') { - return false; - } + if (key === 'actions_summary') return false; if (val) { - return _this.set(key, val); + post.set(key, val); } }); // Rebuild actions summary this.set('actions_summary', Em.A()); if (obj.actions_summary) { - lookup = Em.Object.create(); + var lookup = Em.Object.create(); obj.actions_summary.each(function(a) { var actionSummary; - a.post = _this; + a.post = post; a.actionType = Discourse.get("site").postActionTypeById(a.id); actionSummary = Discourse.ActionSummary.create(a); - _this.get('actions_summary').pushObject(actionSummary); - return lookup.set(a.actionType.get('name_key'), actionSummary); + post.get('actions_summary').pushObject(actionSummary); + lookup.set(a.actionType.get('name_key'), actionSummary); }); - return this.set('actionByName', lookup); + this.set('actionByName', lookup); } }, diff --git a/app/assets/javascripts/discourse/routes/discourse_location.js b/app/assets/javascripts/discourse/routes/discourse_location.js index 777e7def4ab..bb8c55d4367 100644 --- a/app/assets/javascripts/discourse/routes/discourse_location.js +++ b/app/assets/javascripts/discourse/routes/discourse_location.js @@ -70,7 +70,6 @@ Ember.DiscourseLocation = Ember.Object.extend({ */ setURL: function(path) { path = this.formatURL(path); - if (this.getState() && this.getState().path !== path) { popstateReady = true; this.pushState(path);