I have a simple activity viewer in a table that lists activities and their statuses (read or not read) from a db. On the page is a counter that shows how many activities are unread. Each activity gives the option to mark read
or mark unread
depending on the condition. For some reason after the model
is being updated the computed value is not changing on the counter.
Controller
function Activity($scope, $http, $templateCache)
{
$http.get('/activity/get').success(function(data, status)
{
$scope.activities = data;
});
$scope.totalActivities = function()
{
var count = 0;
angular.forEach($scope.activities, function(activity)
{
count += activity.seen == 0 ? 1 : 0;
});
return count;
}
$scope.updateActivity = function(activity)
{
activity.seen = activity.seen == 0 ? 1 : 0;
$http({
method: 'PUT',
url: '/activity/' + activity.id,
data: { seen: activity.seen }
})
.success(function(data, status)
{
console.log(data);
});
}
}
Counter
<span class="badge badge-alert">{{ totalActivities() }}</span>
Table row + button
<tr ng-repeat="activity in activities" ng-class="{ 'activity-read': activity.seen == 1 }">
<td>
<button class="btn btn-default" ng-click="updateActivity(activity)"><span>@{{ 0 == activity.seen ? 'Mark Read' : 'Mark Unread' }}</span></button>
</td>
</tr>
nav template (separate code block)
<div class="account-details" ng-controller="Activity">
@if (Sentry::hasAccess('admin'))
<a href="{{ route('activity.index') }}" class="activity pull-left">
<span class="badge badge-alert">@{{ totalActivities() }}</span>
</a>
@endif
</div>
The counter works fine when first loaded but any updates to the model need a page refresh for the new calculation to show.