2

I'm implementing a notification feature on my app using the ionic framework. the way it works is i make a get request to a rest api that returns an array of notifications. but this seem too much because i have to make a request to the same list everytime, So i was thinking of storing the first request in a local variable (using ngStorage) and just pull up the new items in next requests.. Is this solution possible? if so can you give example on how to go about this

/* Example 1 first request of Json, i'll store the list in a $localStorage variable*/

[{id:1, notification:'notif A'},
{id:2, notification:'notif B'}]

/* For the second request, I only want to push get 'id:3' to my $localStorage how is this possible*/

[{id:1, notification:'notif A'},
{id:2, notification:'notif B'},
{id:3, notification:'notif C'}]
teddybear123
  • 2,314
  • 6
  • 24
  • 38
  • Do you have control over the server that responds to your requests? e.g. Can you pass a `...?last_id=2...` in your second request and rely on the fact that the server will only return new notifications (i.e. `{id:3, ...}`)? – tavnab Jun 23 '15 at 22:32
  • If you have to make the request and retrieve all the data anyway, there is no difference .... – link64 Jun 23 '15 at 22:34
  • I get your point the way the Rest api is setup.. notifications/:begin/:end .........begin and end being the first to last of the last @link64 – teddybear123 Jun 23 '15 at 22:36
  • 1
    You can either use paging on the server and get the items you don't already have. If the server does not support paging you can do an array diff between your stored data and the retrieved data as described here https://stackoverflow.com/questions/1187518/javascript-array-difference (not sure why this would help though since you got all the data) – masimplo Jun 23 '15 at 22:39

1 Answers1

2

It would be better to have the server only pass you the new notifications, provided you can pass some info in your request saying what the newest notification you already have is (e.g. your request can contain a query parameter like last_id=<id>, which your server uses to filter out notifications as old or older than it; this assumes id's value is correlated to the notification age; there are other server-side solutions).

If you don't have control over the server, the fastest thing to do is just replace your whole list with the new list. I don't think there's anything to be gained by finding the deltas & adding them.

However, if you need the deltas for another reason (e.g. you need to run some action on each new notification based on its content), then one option is to store your notifications as an object rather than an array:

// notifications are keyed by ID
var notifications = {};

// load notifications from storage if needed

// newList is the array of new notifications
// newNotificationFcn is an optional callback function, which
// takes the new notification as its only argument
function updateNotifications(newList, newNotificationFcn) {
  var doNotify = (typeof newNotificationFcn === 'function');
  newList.forEach(function(curr) {
    if (!notifications.hasOwnProperty(curr.id) {
      // this is a new notification; do something
      if (doNotify) {
        newNotificationFcn(curr);
      }
      notifications[curr.id] = curr;
    }
  });

  // store away notifications as needed
}

However, this is only if you actually need to do something for those deltas; if all you need to do is keep your list up-to-date, just replace the old one.

tavnab
  • 2,594
  • 1
  • 19
  • 26