From 5c2116a4b3ab66c9d289d72964e64c21bf582078 Mon Sep 17 00:00:00 2001 From: David Baumgold Date: Thu, 9 May 2013 11:10:47 -0400 Subject: [PATCH] Make abstract view, make Alert inherit from it --- cms/static/js/views/feedback.js | 33 +++++++++++++++++++++++++---- cms/static/js/views/server_error.js | 5 +++-- cms/templates/base.html | 4 ++-- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/cms/static/js/views/feedback.js b/cms/static/js/views/feedback.js index 50262267d5..36b4e1d7cb 100644 --- a/cms/static/js/views/feedback.js +++ b/cms/static/js/views/feedback.js @@ -1,17 +1,42 @@ -CMS.Views.Alert = Backbone.View.extend({ - template: _.template($("#alert-tpl").text()), +CMS.Views.SystemFeedback = Backbone.View.extend({ initialize: function() { - this.setElement($("#page-alert")); + this.setElement(document.getElementById(this.id)); this.listenTo(this.model, 'change', this.render); + return this.render(); }, render: function() { this.$el.html(this.template(this.model.attributes)); return this; }, events: { - "click .action-alert-close": "hide" + "click .action-alert-close": "hide", + "click .action-primary": "primaryClick", + "click .action-secondary": "secondaryClick" }, hide: function() { this.model.set("shown", false); + }, + primaryClick: function() { + var primary = this.model.get("actions").primary; + if(primary.click) { + primary.click(this.model); + } + }, + secondaryClick: function(e) { + var secondaryList = this.model.get("actions").secondary; + if(!secondaryList) { + return; + } + // which secondary action was clicked? + var i = _.indexOf(this.$(".action-secondary"), e.target); + var secondary = this.model.get("actions").secondary[i]; + if(secondary.click) { + secondary.click(this.model); + } } }); + +CMS.Views.Alert = CMS.Views.SystemFeedback.extend({ + template: _.template($("#alert-tpl").text()), + id: "page-alert" +}); diff --git a/cms/static/js/views/server_error.js b/cms/static/js/views/server_error.js index e74958992a..4b95bf176f 100644 --- a/cms/static/js/views/server_error.js +++ b/cms/static/js/views/server_error.js @@ -1,9 +1,10 @@ CMS.ServerError = function(model, error) { - var m = new CMS.Models.Alert({ + var m = new CMS.Models.SystemFeedback({ "type": "error", "title": "Server Error", "message": error.responseText, "close": true }); - new CMS.Views.Alert({model: m}).render(); + new CMS.Views.Alert({model: m}); + return m; }; diff --git a/cms/templates/base.html b/cms/templates/base.html index 9aba566ee1..f14e2fa447 100644 --- a/cms/templates/base.html +++ b/cms/templates/base.html @@ -94,8 +94,8 @@ - - + +