Skip to content

Commit e5cd461

Browse files
author
Sébastien Nicouleaud
committed
Render a custom template as flash, with an associated controller
1 parent 5e80cbc commit e5cd461

File tree

4 files changed

+142
-6
lines changed

4 files changed

+142
-6
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,52 @@ App.PostController = Ember.ObjectController.extend({
103103
Please note that whenever you set the flash message from a control it
104104
will be displayed instantly.
105105

106+
### Using flash templates instead of text messages
107+
108+
In case you believe flash is a "view" thing, or if you need to use
109+
different markup through your messages (custom actions, images...),
110+
you can define your flash messages in templates, and render them
111+
using a specific controller.
112+
113+
Example from a route:
114+
115+
```javascript
116+
Ember.ProfileRoute = Ember.Route.extend({
117+
setupController: function(controller, profile) {
118+
if(profile.isEmpty()) {
119+
this.flash({
120+
templateName: 'emptyProfileNotice',
121+
controller: controller,
122+
});
123+
}
124+
this._super(controller, profile);
125+
},
126+
});
127+
```
128+
129+
Example from a controller:
130+
131+
```javascript
132+
App.PostController = Ember.ObjectController.extend({
133+
needs: ['flashMessage'],
134+
135+
actions: {
136+
save: function() {
137+
var flashMessage = this.get('controllers.flashMessage');
138+
var controller = this;
139+
140+
this.get('model').save()
141+
.then(function() {
142+
flashMessage.set('message', Ember.Object.create({
143+
templateName: 'savedPostNotice',
144+
controller: controller,
145+
});
146+
});
147+
}
148+
}
149+
});
150+
```
151+
106152
## Development
107153
108154
This plugin is built with rake pipeline, which requires Ruby. To get

lib/helper.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,20 @@ Ember.Handlebars.registerHelper('flashMessage', function(options) {
99
view;
1010

1111
if (currentMessage) {
12-
view = Ember.View.create({
13-
template: template
14-
});
12+
if (currentMessage.get && currentMessage.get('templateName')) {
13+
var customTemplateName = currentMessage.get('templateName'),
14+
customTemplateController = currentMessage.get('controller');
15+
16+
view = Ember.View.create({
17+
templateName: customTemplateName,
18+
controller: customTemplateController,
19+
});
20+
}
21+
else {
22+
view = Ember.View.create({
23+
template: template
24+
});
25+
}
1526
}
1627

1728
this.set('currentView', view);

lib/route_mixin.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,18 @@ Ember.FlashMessageRouteMixin = Ember.Mixin.create({
1313
controller.set('queuedMessage', messageObject);
1414

1515
return controller;
16-
}
16+
},
17+
18+
flash: function(options) {
19+
var controller = this.controllerFor('flashMessage');
20+
21+
var messageObject = Ember.Object.create({
22+
templateName: options.templateName,
23+
controller: options.controller
24+
});
25+
26+
controller.set('queuedMessage', messageObject);
27+
28+
return controller;
29+
},
1730
});

tests/integration/flash_message_test.js

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,39 @@ App.PromiseRoute = Ember.Route.extend({
2525
App.LoadingRoute = Ember.Route.extend();
2626

2727
App.FromControllerController = Ember.Controller.extend({
28-
needs: 'flashMessage'.w(),
28+
needs: 'flashMessage customFlashTemplate'.w(),
2929

3030
actions: {
3131
showMessage: function() {
3232
var flashMessage = this.get('controllers.flashMessage');
3333
flashMessage.set('message', 'testing');
34-
}
34+
},
35+
36+
showCustomTemplate: function() {
37+
var flashMessage = this.get('controllers.flashMessage');
38+
flashMessage.set('message', Ember.Object.create({
39+
templateName: 'customFlashTemplate',
40+
controller: this.get('controllers.customFlashTemplate'),
41+
}));
42+
},
3543
}
3644
});
3745

46+
App.CustomFlashTemplateController = Ember.Controller.extend({
47+
model: null,
48+
customActionCalled: false,
49+
50+
actions: {
51+
customAction: function() {
52+
this.set('customActionCalled', true);
53+
},
54+
},
55+
});
56+
57+
3858
Ember.TEMPLATES.application = Ember.Handlebars.compile('{{#flashMessage}}<span {{bind-attr class=":message message.type"}}>{{message.text}}</span>{{/flashMessage}}');
59+
Ember.TEMPLATES.customFlashTemplate = Ember.Handlebars.compile('<span class="message custom"}}><a id="custom-flash-template-action" {{action "customAction"}}>Blah</a></span>');
60+
3961

4062
var findMessage = function() {
4163
return $('#qunit-fixture .message');
@@ -211,3 +233,47 @@ test("should be able to use the flash messenger from a controller", function() {
211233
assertMessage();
212234
});
213235
});
236+
237+
test("should be able to render a custom flash template from a route", function() {
238+
visit("/");
239+
240+
andThen(function() {
241+
var customFlashTemplateController = App.__container__.lookup('controller:customFlashTemplate');
242+
243+
customFlashTemplateController.set('model', {name: 'Foo'});
244+
245+
router().flash({
246+
templateName: 'customFlashTemplate',
247+
controller: customFlashTemplateController,
248+
});
249+
});
250+
251+
visit("/posts/new");
252+
253+
andThen(function() {
254+
assertMessage();
255+
ok(hasClass('custom'));
256+
});
257+
});
258+
259+
test("should be able to render a custom flash template from a controller", function() {
260+
visit("/");
261+
262+
andThen(function() {
263+
App.__container__.lookup('controller:fromController')
264+
.send('showCustomTemplate');
265+
});
266+
267+
andThen(function() {
268+
assertMessage();
269+
ok(hasClass('custom'));
270+
});
271+
272+
click("#custom-flash-template-action");
273+
274+
andThen(function() {
275+
var customFlashTemplateController = App.__container__.lookup('controller:customFlashTemplate');
276+
ok(customFlashTemplateController.get('customActionCalled'));
277+
});
278+
});
279+

0 commit comments

Comments
 (0)