I have a notification bar with a counter, it actually prints the user’s “notifications” field value (if it’s greater than 0):
<div id=container> <a href="#" class="activity_link">Activity</a> <?php global $user; $user_fields = user_load($user->uid); $user_notifications = $user_fields->field_notifications['und']['0']['value']; if ($user_notifications > 0) { echo '<div class=blob>'.$user_notifications.'</div>'; echo '<div style="display:none">'.flag_create_link('unmark_notifications', $user->uid).'</div>'; } ?> </div>
And a jQuery function that resets the value to 0 on click:
(function ($) { Drupal.behaviors.scriptjs = { attach: function(context, settings) { $('.activity_link').click(function() { $('.activity_menu').toggle(); $('.blob').fadeOut('normal'); $('.flag-unmark-notifications .flag-action').click(); $('.flag-unmark-notifications .unflag-action').click(); }); } }; })(jQuery);
The problem is that when a new notification is received you need to refresh the page to see it.
What I’ve thought to do is to pass the user’s “notifications” field to jQuery and update accordingly..
Is this the best way to do it or should I think about using Node JS?
How would you listen for a field value change event and then refresh the counter?