1

I want to have a local array that stores "last messages" (inbox view). In my service i do a GET request that returns a data structure like this:

[{id:1, from_user:1, to_user:2, message:"bar", has_been_read:false}, 
{id:2, from_user:3, to_user:2, message:"foo", has_been_read:false}]

The server only sends last messages with has_been_read == false, this is the code for the service:

$scope.unreadMessages = []

$scope.GetUnreadMessages = function(){

      $localStorage.AllMessages.push($scope.unreadMessages)

       UserService.GetUnreadMessages()
            .success(function (data) {

              data = angular.fromJson(data);
              $scope.unreadMessages.push(data);

              }).
            error(function(error) {         
        //do something
          });
}

$scope.GetUnreadMessages()

Let's say In my given example json above I read id:1 which cause the, has_been_read to become true.. on the next request it will only return the data id:2, which is correct.. but my problem is the new request data replaces ALL my old data. so what i want to happen is, even if i read id:1 unless i deleted it or i have a new message from the same user, I still want to keep it when i make new requests.. I know my code is wrong just don't know how to go about this..

6
  • 1
    You need to save to local storage read messages. Commented Jul 1, 2015 at 17:54
  • @dfsq can you please give example code im really new to programming Commented Jul 1, 2015 at 18:06
  • Post the code how you mark message read. Commented Jul 1, 2015 at 18:07
  • @dfsq when i make a Get Request for the conversation of a specific user, the backend sends all of them as true.. so upon clicking the item in my inbox it changes to true.. Commented Jul 1, 2015 at 18:12
  • Ideally the server should send you both read/unread messages, for it is the entity that will keep your clients in sync. With your current implementation each client will exclusively have its own copy of read messages. Commented Jul 1, 2015 at 18:18

1 Answer 1

1

One possible solution is to save the responses grouped by from_user:

.success(function(unreadMessages){
    angular.forEach(unreadMessages, function(unreadMessage){
        // if conversation from a user exists
        if(localStorage.getItem(unreadMessage.from_user)){
            var conversation = JSON.parse(localStorage.getItem(unreadMessage.from_user));
            conversation.push(unreadMessage);
            localStorage.setItem(unreadMessage.from_user, JSON.stringify(conversation));
        } else {
            var conversation = [];
            conversation.push(unreadMessage);
            localStorage.setItem(unreadMessage.from_user, JSON.stringify(conversation));
        }
    });
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.