From 4f3240e488c9ff938447e7ea23f072a836407e89 Mon Sep 17 00:00:00 2001 From: "E. Kolpakov" Date: Fri, 10 Oct 2014 17:31:30 +0400 Subject: [PATCH] Fixed errors in discussion XModule, caused by c2716d4 [TNL-606] Co-Authored-By: jmclaus Co-Authored-By: jsa --- .../view/discussion_thread_edit_view_spec.js | 12 ++----- .../discussion/view/new_post_view_spec.coffee | 5 ++- .../discussion/discussion_module_view.coffee | 6 +++- .../views/discussion_thread_edit_view.js | 34 ++++++++----------- .../views/discussion_thread_view.coffee | 14 ++++++++ .../src/discussion/views/new_post_view.coffee | 4 +-- 6 files changed, 40 insertions(+), 35 deletions(-) diff --git a/common/static/coffee/spec/discussion/view/discussion_thread_edit_view_spec.js b/common/static/coffee/spec/discussion/view/discussion_thread_edit_view_spec.js index 2f1e07b8d2..a75303ca2e 100644 --- a/common/static/coffee/spec/discussion/view/discussion_thread_edit_view_spec.js +++ b/common/static/coffee/spec/discussion/view/discussion_thread_edit_view_spec.js @@ -29,10 +29,7 @@ testUpdate = function(view, thread) { spyOn($, 'ajax').andCallFake(function(params) { expect(params.url.path()).toEqual(DiscussionUtil.urlFor('update_thread', 'dummy_id')); - if (view.isTabMode()) { - // TODO remove the tabMode condition, depends on #5554 / TNL-606 - expect(params.data.thread_type).toBe('discussion'); - } + expect(params.data.thread_type).toBe('discussion'); expect(params.data.commentable_id).toBe('other_topic'); expect(params.data.title).toBe('changed thread title'); params.success(); @@ -45,15 +42,12 @@ expect($.ajax).toHaveBeenCalled(); expect(thread.get('title')).toBe('changed thread title'); - if (view.isTabMode()) { - // TODO remove the tabMode condition, depends on #5554 / TNL-606 - expect(thread.get('thread_type')).toBe('discussion'); - } + expect(thread.get('thread_type')).toBe('discussion'); expect(thread.get('commentable_id')).toBe('other_topic'); expect(thread.get('courseware_title')).toBe('Other Topic'); expect(view.$('.edit-post-title')).toHaveValue(''); expect(view.$('.wmd-preview p')).toHaveText(''); - } + }; it('can save new data correctly in tab mode', function() { this.createEditView(); diff --git a/common/static/coffee/spec/discussion/view/new_post_view_spec.coffee b/common/static/coffee/spec/discussion/view/new_post_view_spec.coffee index 62120e27b1..bb12a5d659 100644 --- a/common/static/coffee/spec/discussion/view/new_post_view_spec.coffee +++ b/common/static/coffee/spec/discussion/view/new_post_view_spec.coffee @@ -127,9 +127,8 @@ describe "NewPostView", -> view.$(".cancel").click() expect(eventSpy).toHaveBeenCalled() expect(view.$(".post-errors").html()).toEqual(""); - if mode == "tab" - expect($("input[id$='post-type-question']")).toBeChecked() - expect($("input[id$='post-type-discussion']")).not.toBeChecked() + expect($("input[id$='post-type-question']")).toBeChecked() + expect($("input[id$='post-type-discussion']")).not.toBeChecked() expect(view.$(".js-post-title").val()).toEqual(""); expect(view.$(".js-post-body textarea").val()).toEqual(""); expect(view.$(".js-follow")).toBeChecked() diff --git a/common/static/coffee/src/discussion/discussion_module_view.coffee b/common/static/coffee/src/discussion/discussion_module_view.coffee index 6bff86635f..6986b114d4 100644 --- a/common/static/coffee/src/discussion/discussion_module_view.coffee +++ b/common/static/coffee/src/discussion/discussion_module_view.coffee @@ -99,13 +99,17 @@ if Backbone? @newPostForm = $('.new-post-article') @threadviews = @discussion.map (thread) => - new DiscussionThreadView( + view = new DiscussionThreadView( el: @$("article#thread_#{thread.id}"), model: thread, mode: "inline", course_settings: @course_settings, topicId: discussionId ) + thread.on "thread:thread_type_updated", -> + view.rerender() + view.expand() + return view _.each @threadviews, (dtv) -> dtv.render() DiscussionUtil.bulkUpdateContentInfo(window.$$annotated_content_info) @newPostView = new NewPostView( diff --git a/common/static/coffee/src/discussion/views/discussion_thread_edit_view.js b/common/static/coffee/src/discussion/views/discussion_thread_edit_view.js index 13eb8bc57a..316c3aff4f 100644 --- a/common/static/coffee/src/discussion/views/discussion_thread_edit_view.js +++ b/common/static/coffee/src/discussion/views/discussion_thread_edit_view.js @@ -28,11 +28,9 @@ this.template = _.template($('#thread-edit-template').html()); this.$el.html(this.template(this.model.toJSON())).appendTo(this.container); this.submitBtn = this.$('.post-update'); - if (this.isTabMode()) { - threadTypeTemplate = _.template($("#thread-type-template").html()); - this.addField(threadTypeTemplate({form_id: formId})); - this.$("#" + formId + "-post-type-" + this.threadType).attr('checked', true); - } + threadTypeTemplate = _.template($("#thread-type-template").html()); + this.addField(threadTypeTemplate({form_id: formId})); + this.$("#" + formId + "-post-type-" + this.threadType).attr('checked', true); this.topicView = new DiscussionTopicMenuView({ topicId: this.topicId, course_settings: this.course_settings @@ -55,7 +53,13 @@ var title = this.$('.edit-post-title').val(), threadType = this.$(".post-type-input:checked").val(), body = this.$('.edit-post-body textarea').val(), - commentableId = this.topicView.getCurrentTopicId(); + commentableId = this.topicView.getCurrentTopicId(), + postData = { + title: title, + thread_type: threadType, + body: body, + commentable_id: commentableId + }; return DiscussionUtil.safeAjax({ $elem: this.submitBtn, @@ -64,28 +68,18 @@ type: 'POST', dataType: 'json', async: false, // @TODO when the rest of the stuff below is made to work properly.. - data: { - title: title, - thread_type: threadType, - body: body, - commentable_id: commentableId - }, + data: postData, error: DiscussionUtil.formErrorHandler(this.$('.post-errors')), success: function() { - var newAttrs = { - title: title, - body: body, - thread_type: threadType, - commentable_id: commentableId, - courseware_title: this.topicView.getFullTopicName() - }; // @TODO: Move this out of the callback, this makes it feel sluggish this.$('.edit-post-title').val('').attr('prev-text', ''); this.$('.edit-post-body textarea').val('').attr('prev-text', ''); this.$('.wmd-preview p').html(''); - this.model.set(newAttrs).unset('abbreviatedBody'); + postData.courseware_title = this.topicView.getFullTopicName(); + this.model.set(postData).unset('abbreviatedBody'); this.trigger('thread:updated'); if (this.threadType !== threadType) { + this.model.set("thread_type", threadType) this.model.trigger('thread:thread_type_updated'); this.trigger('comment:endorse'); } diff --git a/common/static/coffee/src/discussion/views/discussion_thread_view.coffee b/common/static/coffee/src/discussion/views/discussion_thread_view.coffee index 1fac688a1e..97346949c9 100644 --- a/common/static/coffee/src/discussion/views/discussion_thread_view.coffee +++ b/common/static/coffee/src/discussion/views/discussion_thread_view.coffee @@ -34,6 +34,20 @@ if Backbone? if @isQuestion() @markedAnswers = new Comments() + rerender: () -> + if @showView? + @showView.undelegateEvents() + @undelegateEvents() + @$el.empty() + @initialize( + mode: @mode + model: @model + el: @el + course_settings: @course_settings + topicId: @topicId + ) + @render() + renderTemplate: -> @template = _.template($("#thread-template").html()) @template(@model.toJSON()) diff --git a/common/static/coffee/src/discussion/views/new_post_view.coffee b/common/static/coffee/src/discussion/views/new_post_view.coffee index 3665c526cd..1fc3cb2813 100644 --- a/common/static/coffee/src/discussion/views/new_post_view.coffee +++ b/common/static/coffee/src/discussion/views/new_post_view.coffee @@ -16,9 +16,9 @@ if Backbone? form_id: @mode + (if @topicId then "-" + @topicId else "") }) @$el.html(_.template($("#new-post-template").html(), context)) + threadTypeTemplate = _.template($("#thread-type-template").html()); + @addField(threadTypeTemplate({form_id: _.uniqueId("form-")})); if @isTabMode() - threadTypeTemplate = _.template($("#thread-type-template").html()); - @addField(threadTypeTemplate({form_id: _.uniqueId("form-")})); @topicView = new DiscussionTopicMenuView { topicId: @topicId course_settings: @course_settings