2015-05-06 3 views
0

Я - атмосфера & Угловой новичок, и я действительно изо всех сил пытаюсь найти ответ на это! Возможно, я задаю неправильный вопрос.Атмосфера и угловой JS как

Я настраиваю уведомления, используя Атмосфера. Я могу открыть websocket и посмотреть, как обновления происходят, если я отправляю URL-адрес API прямо в свой браузер.

В Angular У меня есть цикл ng-repeat, который я хотел бы запускать, поскольку каждое новое обновление добавляет новый объект в websocket.

<li ng-repeat="notification in notifications track by $index">

Я использую угловую часы, чтобы проверить наличие обновлений, но не подхватывает новые объекты добавляются в массив. Вот мой код:

// notification alerts 
$scope.notifications = []; 

notificationsService.notificationAlerts().then(function success(response) {     
    var jsonStringArray = response.data.split('|'); 
    $scope.notifications = $.map(jsonStringArray, function(n, i){ 
     if (n !== ""){ 
      return JSON.parse(n); 
     } 
    }); 
    console.log('Connect', response);    
}); 

$scope.$watch('notifications', function(newVal, oldVal){ 
    console.log('Watch', $scope.notifications); 
}, true); 

Надеюсь, я сделал себе ясно, дайте мне знать, если мне нужно разработать, или если я прошу неправильный вопрос. Благодаря!

ответ

1

ОК, мне удалось решить эту проблему, для тех, кто наткнулся на нее позже. Вот окончательный JS:

// add number of notifications to ".notifications-number" 
function updateNumberOfNotifications(){ 
    var numberOfNotifications = $("ul.notifications-list li").not(".nocount").length; 
    if (numberOfNotifications < 1) { 
     $(".notifications-number, .notifications-list").addClass("hidden"); 
    } else { 
     $(".notifications-number").html(numberOfNotifications); 
     $(".notifications-number, .notifications-list").removeClass("hidden"); 
    }     
}    

// notification alert variables 
$scope.notifications = []; 
var socket = atmosphere; 
var subSocket; 

// subscribe 
function subscribe() { 
    var request = { 
     url : "/service/notifier", 
     transport: 'long-polling' 
    }; 

    request.onMessage = function (response) { 
     //console.log('response', response); 
     var jsonStringArray = response.responseBody.split('|'); 
     // console.log('json string array', jsonStringArray); 
     $.each(jsonStringArray, function(index, elem){ 
      if (elem != ""){ 
       $scope.notifications.push(JSON.parse(elem)); 
       console.log("object", JSON.parse(elem)); 
      }       
     }); 
     //$scope.notifications.push($scope.newNotification); 
     $scope.$apply();     

     updateNumberOfNotifications();   

     // console.log('$scope.notifications', $scope.notifications);    
    }; 

    subSocket = socket.subscribe(request); 
} 

function unsubscribe(){ 
    socket.unsubscribe(); 
} 

// subscribe on load and update notifications 
updateNumberOfNotifications(); 
subscribe(); 
Смежные вопросы