@@ -125,47 +125,37 @@ def locateToRoom(organization, room_name)
125
125
def webView ( sender , didFinishLoadForFrame :frame )
126
126
sender . windowScriptObject . evaluateWebScript <<-CODE
127
127
(function(){
128
- var onMessageCreated = function(data){
129
- var notify = false;
130
- var mode = window.butter.notificationMode();
131
- if (mode == "all") {
132
- notify = true;
133
- } else if (mode == "mention") {
134
- if (data.message.mentions.indexOf(parseInt(window.Idobata.user.id)) >= 0) {
128
+ var onMessageCreated = function(user) {
129
+ return function(data){
130
+ var notify = false;
131
+ var mode = window.butter.notificationMode();
132
+ if (mode == "all") {
135
133
notify = true;
134
+ } else if (mode == "mention") {
135
+ if (data.message.mentions.indexOf(parseInt(user.get('id'))) >= 0) {
136
+ notify = true;
137
+ }
136
138
}
137
- }
138
- if ( notify) {
139
- butter.notify(JSON.stringify(data.message));
140
- }
139
+ if (notify) {
140
+ butter. notify(JSON.stringify(data.message));
141
+ }
142
+ };
141
143
};
142
144
143
145
var onUnreadCountUpdated = function() {
144
- var totalUnreadCount = this.get('rooms').reduce(function(acc, room) {
145
- return acc + room.get('unreadMessagesCount');
146
- }, 0);
147
- window.butter.setBadge(totalUnreadCount);
146
+ window.butter.setBadge(this.get('totalUnreadMessagesCount'));
148
147
}
149
148
150
- var bindEvent = function(){
151
- if (!window.Idobata.pusher) {
152
- return false;
153
- }
154
- if (!window.Idobata.user) {
155
- return false;
156
- }
157
- window.Idobata.pusher.bind('message_created', onMessageCreated);
158
- window.Idobata.user.addObserver('[email protected] ', onUnreadCountUpdated);
159
- onUnreadCountUpdated.apply(window.Idobata.user);
160
-
161
- return true;
162
- };
149
+ window.addEventListener('ready.idobata', function(e) {
150
+ var container = e.detail.container;
151
+
152
+ var pusher = container.lookup('pusher:main');
153
+ var user = container.lookup('service:session').get('user');
163
154
164
- setTimeout(function setBind(){
165
- if (!bindEvent()) {
166
- setTimeout(setBind, 100);
167
- }
168
- }, 100);
155
+ pusher.bind('message:created', onMessageCreated(user));
156
+ user.addObserver('totalUnreadMessagesCount', onUnreadCountUpdated);
157
+ onUnreadCountUpdated.apply(user);
158
+ });
169
159
})();
170
160
CODE
171
161
end
0 commit comments