Move alerts to their own controller

This commit is contained in:
Toby Zerner 2015-02-08 15:51:40 +10:30
parent 28d213d868
commit 2d181933ea
8 changed files with 47 additions and 32 deletions

View File

@ -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();
}
}
});

View File

@ -15,20 +15,9 @@ export default Ember.Controller.extend({
searchQuery: '', searchQuery: '',
searchActive: false, searchActive: false,
alerts: [],
actions: { actions: {
search: function(query) { search: function(query) {
this.transitionToRoute('index', {queryParams: {searchQuery: query, sort: query ? 'relevance' : 'recent'}}); 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();
} }
} }
}); });

View File

@ -4,8 +4,8 @@ import ComposerReply from '../components/discussions/composer-reply';
import ActionButton from '../components/ui/controls/action-button'; import ActionButton from '../components/ui/controls/action-button';
import AlertMessage from '../components/alert-message'; import AlertMessage from '../components/alert-message';
export default Ember.ObjectController.extend(Ember.Evented, { export default Ember.Controller.extend(Ember.Evented, {
needs: ['application', 'composer'], needs: ['application', 'alerts', 'composer'],
queryParams: ['start'], queryParams: ['start'],
start: '1', start: '1',
@ -25,7 +25,7 @@ export default Ember.ObjectController.extend(Ember.Evented, {
var stream = this.get('stream'); var stream = this.get('stream');
composer.set('content.loading', true); composer.set('content.loading', true);
controller.get('controllers.application').send('clearAlerts'); controller.get('controllers.alerts').send('clearAlerts');
var post = this.store.createRecord('post', { var post = this.store.createRecord('post', {
content: data.content, content: data.content,
@ -59,7 +59,7 @@ export default Ember.ObjectController.extend(Ember.Evented, {
message: 'Your reply was posted.' message: 'Your reply was posted.'
}); });
message.on('populateControls', function(controls) { message.on('populateControls', function(controls) {
controls.pushObjectWithTag(ActionButton.extend({ controls.pushObjectWithTag(ActionButton.create({
label: 'View', label: 'View',
action: function() { action: function() {
controller.transitionToRoute('discussion', post.get('discussion'), {queryParams: {start: post.get('number')}}); controller.transitionToRoute('discussion', post.get('discussion'), {queryParams: {start: post.get('number')}});
@ -67,7 +67,7 @@ export default Ember.ObjectController.extend(Ember.Evented, {
} }
}), 'view'); }), 'view');
}); });
controller.get('controllers.application').send('alert', message); controller.get('controllers.alerts').send('alert', message);
} }
}, },
function(reason) { function(reason) {
@ -77,7 +77,7 @@ export default Ember.ObjectController.extend(Ember.Evented, {
type: 'warning', type: 'warning',
message: reason.errors[i] message: reason.errors[i]
}); });
controller.get('controllers.application').send('alert', message); controller.get('controllers.alerts').send('alert', message);
} }
}) })
.finally(function() { .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 // If the composer is already set up for this discussion, then we
// don't need to change its content - we can just show it. // 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({ composer.switchContent(ComposerReply.create({
user: controller.get('session.user'), user: controller.get('session.user'),
discussion: discussion, discussion: discussion,

View File

@ -7,7 +7,7 @@ import ComposerDiscussion from '../components/discussions/composer-discussion';
import AlertMessage from '../components/alert-message'; import AlertMessage from '../components/alert-message';
export default Ember.Controller.extend(Ember.Evented, PaneableMixin, { 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'), index: Ember.computed.alias('controllers.index/index'),
@ -19,7 +19,7 @@ export default Ember.Controller.extend(Ember.Evented, PaneableMixin, {
var stream = this.get('stream'); var stream = this.get('stream');
composer.set('content.loading', true); composer.set('content.loading', true);
controller.get('controllers.application').send('clearAlerts'); controller.get('controllers.alerts').send('clearAlerts');
var discussion = this.store.createRecord('discussion', { var discussion = this.store.createRecord('discussion', {
title: data.title, title: data.title,
@ -38,7 +38,7 @@ export default Ember.Controller.extend(Ember.Evented, PaneableMixin, {
type: 'warning', type: 'warning',
message: reason.errors[i] message: reason.errors[i]
}); });
controller.get('controllers.application').send('alert', message); controller.get('controllers.alerts').send('alert', message);
} }
}) })
.finally(function() { .finally(function() {

View File

@ -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')
}
};

View File

@ -0,0 +1,7 @@
<div class="alerts">
{{#each alert in alerts}}
<div class="alert-wrapper">
{{view alert dismiss="dismissAlert"}}
</div>
{{/each}}
</div>

View File

@ -47,12 +47,8 @@
</div> </div>
<div id="modal" class="modal fade">
{{outlet "modal"}} {{outlet "modal"}}
</div>
<div class="alerts"> {{render "alerts"}}
{{#each alert in alerts}}
<div class="alert-wrapper">
{{view alert dismiss="dismissAlert"}}
</div>
{{/each}}
</div>

View File

@ -1,6 +1,6 @@
<header class="hero discussion-hero"> <header class="hero discussion-hero">
<div class="container"> <div class="container">
<h2>{{title}}</h2> <h2>{{model.title}}</h2>
</div> </div>
</header> </header>
@ -13,6 +13,5 @@
viewName="streamContent" viewName="streamContent"
stream=stream stream=stream
class="discussion-posts posts" class="discussion-posts posts"
component="discussions/post-wrapper"
updateStart="updateStart"}} updateStart="updateStart"}}
</div> </div>