From 2d181933ea297b363b6819bddd6381a91b4fbc60 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sun, 8 Feb 2015 15:51:40 +1030 Subject: [PATCH] Move alerts to their own controller --- ember/app/controllers/alerts.js | 17 +++++++++++++++++ ember/app/controllers/application.js | 11 ----------- ember/app/controllers/discussion.js | 14 +++++++------- ember/app/controllers/index.js | 6 +++--- ember/app/initializers/inject-components.js | 7 +++++++ ember/app/templates/alerts.hbs | 7 +++++++ ember/app/templates/application.hbs | 12 ++++-------- ember/app/templates/discussion.hbs | 5 ++--- 8 files changed, 47 insertions(+), 32 deletions(-) create mode 100644 ember/app/controllers/alerts.js create mode 100644 ember/app/initializers/inject-components.js create mode 100644 ember/app/templates/alerts.hbs diff --git a/ember/app/controllers/alerts.js b/ember/app/controllers/alerts.js new file mode 100644 index 000000000..cea222d20 --- /dev/null +++ b/ember/app/controllers/alerts.js @@ -0,0 +1,17 @@ +import Ember from 'ember'; + +export default Ember.Controller.extend({ + alerts: [], + + actions: { + alert: function(message) { + this.get('alerts').pushObject(message); + }, + dismissAlert: function(message) { + this.get('alerts').removeObject(message); + }, + clearAlerts: function() { + this.get('alerts').clear(); + } + } +}); diff --git a/ember/app/controllers/application.js b/ember/app/controllers/application.js index 4d8958a17..f16f2bc35 100644 --- a/ember/app/controllers/application.js +++ b/ember/app/controllers/application.js @@ -15,20 +15,9 @@ export default Ember.Controller.extend({ searchQuery: '', searchActive: false, - alerts: [], - actions: { search: function(query) { this.transitionToRoute('index', {queryParams: {searchQuery: query, sort: query ? 'relevance' : 'recent'}}); - }, - alert: function(message) { - this.get('alerts').pushObject(message); - }, - dismissAlert: function(message) { - this.get('alerts').removeObject(message); - }, - clearAlerts: function() { - this.get('alerts').clear(); } } }); diff --git a/ember/app/controllers/discussion.js b/ember/app/controllers/discussion.js index 3f373802c..a0afeac2e 100644 --- a/ember/app/controllers/discussion.js +++ b/ember/app/controllers/discussion.js @@ -4,8 +4,8 @@ import ComposerReply from '../components/discussions/composer-reply'; import ActionButton from '../components/ui/controls/action-button'; import AlertMessage from '../components/alert-message'; -export default Ember.ObjectController.extend(Ember.Evented, { - needs: ['application', 'composer'], +export default Ember.Controller.extend(Ember.Evented, { + needs: ['application', 'alerts', 'composer'], queryParams: ['start'], start: '1', @@ -25,7 +25,7 @@ export default Ember.ObjectController.extend(Ember.Evented, { var stream = this.get('stream'); composer.set('content.loading', true); - controller.get('controllers.application').send('clearAlerts'); + controller.get('controllers.alerts').send('clearAlerts'); var post = this.store.createRecord('post', { content: data.content, @@ -59,7 +59,7 @@ export default Ember.ObjectController.extend(Ember.Evented, { message: 'Your reply was posted.' }); message.on('populateControls', function(controls) { - controls.pushObjectWithTag(ActionButton.extend({ + controls.pushObjectWithTag(ActionButton.create({ label: 'View', action: function() { controller.transitionToRoute('discussion', post.get('discussion'), {queryParams: {start: post.get('number')}}); @@ -67,7 +67,7 @@ export default Ember.ObjectController.extend(Ember.Evented, { } }), 'view'); }); - controller.get('controllers.application').send('alert', message); + controller.get('controllers.alerts').send('alert', message); } }, function(reason) { @@ -77,7 +77,7 @@ export default Ember.ObjectController.extend(Ember.Evented, { type: 'warning', message: reason.errors[i] }); - controller.get('controllers.application').send('alert', message); + controller.get('controllers.alerts').send('alert', message); } }) .finally(function() { @@ -93,7 +93,7 @@ export default Ember.ObjectController.extend(Ember.Evented, { // If the composer is already set up for this discussion, then we // don't need to change its content - we can just show it. - if (!(composer.get('content') instanceof ComposerReply) || composer.get('content.discussion') != discussion) { + if (!(composer.get('content') instanceof ComposerReply) || composer.get('content.discussion') !== discussion) { composer.switchContent(ComposerReply.create({ user: controller.get('session.user'), discussion: discussion, diff --git a/ember/app/controllers/index.js b/ember/app/controllers/index.js index 9a6f12e13..b50b14be4 100644 --- a/ember/app/controllers/index.js +++ b/ember/app/controllers/index.js @@ -7,7 +7,7 @@ import ComposerDiscussion from '../components/discussions/composer-discussion'; import AlertMessage from '../components/alert-message'; export default Ember.Controller.extend(Ember.Evented, PaneableMixin, { - needs: ['application', 'composer', 'index/index', 'discussion'], + needs: ['application', 'composer', 'alerts', 'index/index', 'discussion'], index: Ember.computed.alias('controllers.index/index'), @@ -19,7 +19,7 @@ export default Ember.Controller.extend(Ember.Evented, PaneableMixin, { var stream = this.get('stream'); composer.set('content.loading', true); - controller.get('controllers.application').send('clearAlerts'); + controller.get('controllers.alerts').send('clearAlerts'); var discussion = this.store.createRecord('discussion', { title: data.title, @@ -38,7 +38,7 @@ export default Ember.Controller.extend(Ember.Evented, PaneableMixin, { type: 'warning', message: reason.errors[i] }); - controller.get('controllers.application').send('alert', message); + controller.get('controllers.alerts').send('alert', message); } }) .finally(function() { diff --git a/ember/app/initializers/inject-components.js b/ember/app/initializers/inject-components.js new file mode 100644 index 000000000..e52a31521 --- /dev/null +++ b/ember/app/initializers/inject-components.js @@ -0,0 +1,7 @@ +export default { + name: 'inject-components', + initialize: function(container, application) { + application.inject('component', 'alerts', 'controller:alerts') + application.inject('component', 'composer', 'controller:composer') + } +}; diff --git a/ember/app/templates/alerts.hbs b/ember/app/templates/alerts.hbs new file mode 100644 index 000000000..cd530e189 --- /dev/null +++ b/ember/app/templates/alerts.hbs @@ -0,0 +1,7 @@ +
+ {{#each alert in alerts}} +
+ {{view alert dismiss="dismissAlert"}} +
+ {{/each}} +
diff --git a/ember/app/templates/application.hbs b/ember/app/templates/application.hbs index da3c32a6e..8f058d102 100644 --- a/ember/app/templates/application.hbs +++ b/ember/app/templates/application.hbs @@ -47,12 +47,8 @@ -{{outlet "modal"}} - -
- {{#each alert in alerts}} -
- {{view alert dismiss="dismissAlert"}} -
- {{/each}} + + +{{render "alerts"}} diff --git a/ember/app/templates/discussion.hbs b/ember/app/templates/discussion.hbs index 42e8ee17d..cba3c46e3 100644 --- a/ember/app/templates/discussion.hbs +++ b/ember/app/templates/discussion.hbs @@ -1,6 +1,6 @@
-

{{title}}

+

{{model.title}}

@@ -13,6 +13,5 @@ viewName="streamContent" stream=stream class="discussion-posts posts" - component="discussions/post-wrapper" updateStart="updateStart"}} -
\ No newline at end of file +