From 9ec33928ce0d69cdd406a9602f9edec4c113d55f Mon Sep 17 00:00:00 2001 From: cahrens Date: Thu, 7 Mar 2013 13:37:10 -0500 Subject: [PATCH 1/2] New version of Backbone has separate invalid event. --- cms/static/js/views/settings/advanced_view.js | 3 ++- cms/static/js/views/settings/main_settings_view.js | 3 ++- .../js/views/settings/settings_grading_view.js | 5 +++-- cms/static/js/views/validating_view.js | 14 +++----------- 4 files changed, 10 insertions(+), 15 deletions(-) diff --git a/cms/static/js/views/settings/advanced_view.js b/cms/static/js/views/settings/advanced_view.js index d20a21f7e7..2f2abb8d25 100644 --- a/cms/static/js/views/settings/advanced_view.js +++ b/cms/static/js/views/settings/advanced_view.js @@ -31,7 +31,8 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ // because these are outside of this.$el, they can't be in the event hash $('.save-button').on('click', this, this.saveView); $('.cancel-button').on('click', this, this.revertView); - this.model.on('error', this.handleValidationError, this); + this.listenTo(this.model, 'error', CMS.ServerError); + this.listenTo(this.model, 'invalid', this.handleValidationError); }, render: function() { // catch potential outside call before template loaded diff --git a/cms/static/js/views/settings/main_settings_view.js b/cms/static/js/views/settings/main_settings_view.js index 8f998dbf7a..9bd8feab8c 100644 --- a/cms/static/js/views/settings/main_settings_view.js +++ b/cms/static/js/views/settings/main_settings_view.js @@ -26,7 +26,8 @@ CMS.Views.Settings.Details = CMS.Views.ValidatingView.extend({ var dateIntrospect = new Date(); this.$el.find('#timezone').html("(" + dateIntrospect.getTimezone() + ")"); - this.model.on('error', this.handleValidationError, this); + this.listenTo(this.model, 'error', CMS.ServerError); + this.listenTo(this.model, 'invalid', this.handleValidationError); this.selectorToField = _.invert(this.fieldToSelectorMap); }, diff --git a/cms/static/js/views/settings/settings_grading_view.js b/cms/static/js/views/settings/settings_grading_view.js index a7c8defb43..6ef01d8ade 100644 --- a/cms/static/js/views/settings/settings_grading_view.js +++ b/cms/static/js/views/settings/settings_grading_view.js @@ -44,7 +44,8 @@ CMS.Views.Settings.Grading = CMS.Views.ValidatingView.extend({ self.render(); } ); - this.model.on('error', this.handleValidationError, this); + this.listenTo(this.model, 'error', CMS.ServerError); + this.listenTo(this.model, 'invalid', this.handleValidationError); this.model.get('graders').on('remove', this.render, this); this.model.get('graders').on('reset', this.render, this); this.model.get('graders').on('add', this.render, this); @@ -316,7 +317,7 @@ CMS.Views.Settings.GraderView = CMS.Views.ValidatingView.extend({ 'blur :input' : "inputUnfocus" }, initialize : function() { - this.model.on('error', this.handleValidationError, this); + this.listenTo(this.model, 'invalid', this.handleValidationError); this.selectorToField = _.invert(this.fieldToSelectorMap); this.render(); }, diff --git a/cms/static/js/views/validating_view.js b/cms/static/js/views/validating_view.js index e4928a8ebe..041e779030 100644 --- a/cms/static/js/views/validating_view.js +++ b/cms/static/js/views/validating_view.js @@ -3,7 +3,8 @@ CMS.Views.ValidatingView = Backbone.View.extend({ // decorates the fields. Needs wiring per class, but this initialization shows how // either have your init call this one or copy the contents initialize : function() { - this.model.on('error', this.handleValidationError, this); + this.listenTo(this.model, 'error', CMS.ServerError); + this.listenTo(this.model, 'invalid', this.handleValidationError); this.selectorToField = _.invert(this.fieldToSelectorMap); }, @@ -18,20 +19,11 @@ CMS.Views.ValidatingView = Backbone.View.extend({ // which may be the subjects of validation errors }, _cacheValidationErrors : [], + handleValidationError : function(model, error) { - // error triggered either by validation or server error // error is object w/ fields and error strings for (var field in error) { var ele = this.$el.find('#' + this.fieldToSelectorMap[field]); - if (ele.length === 0) { - // check if it might a server error: note a typo in the field name - // or failure to put in a map may cause this to muffle validation errors - if (_.has(error, 'error') && _.has(error, 'responseText')) { - CMS.ServerError(model, error); - return; - } - else continue; - } this._cacheValidationErrors.push(ele); if ($(ele).is('div')) { // put error on the contained inputs From 98e410816ed51ecfb2e6e68b558d604b76753148 Mon Sep 17 00:00:00 2001 From: cahrens Date: Thu, 7 Mar 2013 13:52:55 -0500 Subject: [PATCH 2/2] New version of Backbone has separate invalid event. --- cms/static/js/views/settings/settings_grading_view.js | 1 + 1 file changed, 1 insertion(+) diff --git a/cms/static/js/views/settings/settings_grading_view.js b/cms/static/js/views/settings/settings_grading_view.js index 6ef01d8ade..78972f97a7 100644 --- a/cms/static/js/views/settings/settings_grading_view.js +++ b/cms/static/js/views/settings/settings_grading_view.js @@ -317,6 +317,7 @@ CMS.Views.Settings.GraderView = CMS.Views.ValidatingView.extend({ 'blur :input' : "inputUnfocus" }, initialize : function() { + this.listenTo(this.model, 'error', CMS.ServerError); this.listenTo(this.model, 'invalid', this.handleValidationError); this.selectorToField = _.invert(this.fieldToSelectorMap); this.render();