-
Notifications
You must be signed in to change notification settings - Fork 23
/
flash-message.js
84 lines (68 loc) · 2.22 KB
/
flash-message.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
Ember.FlashMessageController = Ember.Controller.extend({
queuedMessage: null,
currentMessage: null,
message: Ember.computed.alias('currentMessage'),
now: function() {
this.setProperties({
queuedMessage: null,
currentMessage: this.get('queuedMessage')
});
},
actions: {
dismissFlashMessage: function() {
this.set('currentMessage', null);
}
}
});
Ember.Handlebars.registerHelper('flashMessage', function(options) {
var container = this._keywords.view.container,
controller = container.lookup('controller:flashMessage'),
parent = Ember.ContainerView.extend({
hideAndShowMessage: function() {
var currentMessage = this.get('controller.currentMessage'),
view;
if (currentMessage) {
view = Ember.View.create({
template: this.get('template')
});
}
this.set('currentView', view);
}.observes('controller.currentMessage')
});
options.hash.controller = controller;
options.hashTypes = options.hashTypes || {};
Ember.Handlebars.helpers.view.helperFunction.call(this, [parent], options.hash, options, options);
});
Ember.Application.initializer({
name: 'flashMessage',
initialize: function(container, application) {
container.register('controller:flashMessage', Ember.FlashMessageController);
}
});
Ember.FlashMessageRouteMixin = Ember.Mixin.create({
flashMessage: function(message, messageType) {
var controller = this.controllerFor('flashMessage');
var messageObject = Ember.Object.create({
text: message
});
if(typeof messageType !== 'undefined') {
messageObject.set('type', messageType);
}
controller.set('queuedMessage', messageObject);
return controller;
}
});
Ember.Route.reopen(
Ember.FlashMessageRouteMixin, {
enter: function() {
this._super.apply(this, arguments);
var controller = this.controllerFor('flashMessage'),
routeName = this.get('routeName');
var target = this.get('router.router.activeTransition.targetName');
// do not display message in loading route, wait until
// any loading is done.
if (routeName !== "loading" && routeName === target) {
controller.now();
}
}
});