From fdbf63920179170c5dbdb4acd03793469888ed1e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?=
 <lorenz-dev@lgh-alumni.de>
Date: Tue, 15 Apr 2014 22:01:13 +0200
Subject: [PATCH] Don't modify buffer in document visibility change handler if
 not connected

---
 js/glowingbear.js | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/js/glowingbear.js b/js/glowingbear.js
index cee696f..0401427 100644
--- a/js/glowingbear.js
+++ b/js/glowingbear.js
@@ -582,11 +582,14 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
                 // We just switched back to the glowing-bear window and unread messages may have
                 // accumulated in the active buffer while the window was in the background
                 var buffer = models.getActiveBuffer();
-                buffer.unread = 0;
-                buffer.notification = 0;
+                // This can also be triggered before connecting to the relay, check for null (not undefined!)
+                if (buffer !== null) {
+                    buffer.unread = 0;
+                    buffer.notification = 0;
 
-                // Trigger title and favico update
-                $rootScope.$emit('notificationChanged');
+                    // Trigger title and favico update
+                    $rootScope.$emit('notificationChanged');
+                }
 
                 // the unread badge in the bufferlist doesn't update if we don't do this
                 $rootScope.$apply();