From 577a8b192f96a9551c2c708b7a503985d9262c8a Mon Sep 17 00:00:00 2001 From: Fergal Moran Date: Sat, 23 Nov 2013 23:39:51 +0000 Subject: [PATCH] Fixed counts in notification dropdown fixes #6 --- spa/api/v1/NotificationResource.py | 2 +- .../notificationCollection.coffee | 4 ++++ .../notifications/notificationCollection.js | 4 ++++ .../notificationsListView.coffee | 10 +++++--- .../notifications/notificationsListView.js | 23 +++++++++++-------- templates/views/NotificationsListView.html | 4 ++-- 6 files changed, 31 insertions(+), 16 deletions(-) diff --git a/spa/api/v1/NotificationResource.py b/spa/api/v1/NotificationResource.py index 2aae55a..59e71e2 100644 --- a/spa/api/v1/NotificationResource.py +++ b/spa/api/v1/NotificationResource.py @@ -29,5 +29,5 @@ class NotificationResource(BackboneCompatibleResource): return bundle def alter_list_data_to_serialize(self, request, data): - data['meta']['is_new'] = 5 #Notification.objects.filter(to_user=request.user, accepted_date__isnull=True).count() + data['meta']['is_new'] = Notification.objects.filter(to_user=request.user, accepted_date__isnull=True).count() return data \ No newline at end of file diff --git a/static/js/app/models/notifications/notificationCollection.coffee b/static/js/app/models/notifications/notificationCollection.coffee index 14a84b6..ef38b3a 100644 --- a/static/js/app/models/notifications/notificationCollection.coffee +++ b/static/js/app/models/notifications/notificationCollection.coffee @@ -6,6 +6,10 @@ define ['backbone', 'vent', page: 0 model: NotificationItem limit: 5 + + newCount: -> + return @is_new + url: -> com.podnoms.settings.urlRoot + "notification/?limit=" + @limit + "&offset=" + Math.max(@page - 1, 0) * @limit diff --git a/static/js/app/models/notifications/notificationCollection.js b/static/js/app/models/notifications/notificationCollection.js index fd60040..1358d39 100644 --- a/static/js/app/models/notifications/notificationCollection.js +++ b/static/js/app/models/notifications/notificationCollection.js @@ -19,6 +19,10 @@ NotificationCollection.prototype.limit = 5; + NotificationCollection.prototype.newCount = function() { + return this.is_new; + }; + NotificationCollection.prototype.url = function() { return com.podnoms.settings.urlRoot + "notification/?limit=" + this.limit + "&offset=" + Math.max(this.page - 1, 0) * this.limit; }; diff --git a/static/js/app/views/notifications/notificationsListView.coffee b/static/js/app/views/notifications/notificationsListView.coffee index 15b26b4..09d57ee 100644 --- a/static/js/app/views/notifications/notificationsListView.coffee +++ b/static/js/app/views/notifications/notificationsListView.coffee @@ -13,7 +13,8 @@ define ['marionette', 'underscore', 'vent', 'utils', "click #notifications-dropdown": "showNotifications" ui: notificationSurround: "#notification-surround" - notificationCount: "#notification-count-badge" + notificationCountBadge: "#notification-count-badge" + notificationCount: "#notification-count" initialize: => #quick and dirty check to see if user is logged in @@ -29,10 +30,13 @@ define ['marionette', 'underscore', 'vent', 'utils', ) renderBeacon: (model) -> - $(@ui.notificationCount).text(@collection.meta.is_new) - if @collection.meta.is_new == 0 + newCount = @collection.meta.is_new + if newCount == 0 + $(@ui.notificationCount).text("Notifications") $(@ui.notificationSurround).hide() else + $(@ui.notificationCountBadge).text(newCount) + $(@ui.notificationCount).text(newCount + " Notifications") $(@ui.notificationSurround).show() $(@ui.notificationSurround).addClass('animate pulse') if model diff --git a/static/js/app/views/notifications/notificationsListView.js b/static/js/app/views/notifications/notificationsListView.js index 31e4388..2fb52a1 100644 --- a/static/js/app/views/notifications/notificationsListView.js +++ b/static/js/app/views/notifications/notificationsListView.js @@ -1,18 +1,18 @@ -// Generated by CoffeeScript 1.6.2 +// Generated by CoffeeScript 1.4.0 (function() { var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; define(['marionette', 'underscore', 'vent', 'utils', 'models/notifications/notificationCollection', 'views/notifications/notificationsItemView', 'text!/tpl/NotificationsListView'], function(Marionette, _, vent, utils, NotificationCollection, NotificationsItemView, Template) { - var NotificationsListView, _ref; - + var NotificationsListView; NotificationsListView = (function(_super) { + __extends(NotificationsListView, _super); function NotificationsListView() { - this.initialize = __bind(this.initialize, this); _ref = NotificationsListView.__super__.constructor.apply(this, arguments); - return _ref; + this.initialize = __bind(this.initialize, this); + return NotificationsListView.__super__.constructor.apply(this, arguments); } NotificationsListView.prototype.template = _.template(Template); @@ -29,12 +29,12 @@ NotificationsListView.prototype.ui = { notificationSurround: "#notification-surround", - notificationCount: "#notification-count-badge" + notificationCountBadge: "#notification-count-badge", + notificationCount: "#notification-count" }; NotificationsListView.prototype.initialize = function() { var _this = this; - this.collection = new NotificationCollection; return this.collection.fetch({ success: function() { @@ -53,10 +53,14 @@ }; NotificationsListView.prototype.renderBeacon = function(model) { - $(this.ui.notificationCount).text(this.collection.meta.is_new); - if (this.collection.meta.is_new === 0) { + var newCount; + newCount = this.collection.meta.is_new; + if (newCount === 0) { + $(this.ui.notificationCount).text("Notifications"); return $(this.ui.notificationSurround).hide(); } else { + $(this.ui.notificationCountBadge).text(newCount); + $(this.ui.notificationCount).text(newCount + " Notifications"); $(this.ui.notificationSurround).show(); $(this.ui.notificationSurround).addClass('animate pulse'); if (model) { @@ -67,7 +71,6 @@ NotificationsListView.prototype.showNotifications = function() { var _this = this; - console.log("NotificationsListView: showNotifications"); return $.ajax({ url: '/ajax/mark_read/', diff --git a/templates/views/NotificationsListView.html b/templates/views/NotificationsListView.html index 483067f..e675be8 100644 --- a/templates/views/NotificationsListView.html +++ b/templates/views/NotificationsListView.html @@ -1,4 +1,4 @@ - + @@ -6,6 +6,6 @@