Save only changed objects on server: Restful service + angularjs

I am displaying a list of posts on my page with a button that the user can turn on or off. In order to throttle traffic using my server, I would like:

  • send updates for all messages in which a similar status has been changed from once in the same request
  • send updates for modified posts only
  • update the status at least every N seconds and only on the page. Not every time a user toggles a similar button.

Is this possible with corner ones?

+3


source to share


1 answer


I wrote an example which can be seen here:

http://plnkr.co/edit/imMGTJ75mKJZT7ispD9E?p=preview

I spent some time on this with the comments and provided useful information that is displayed on the page itself when it starts. Change the delays if you want it to run slower if the messages are moving too fast.

From your question, it looks like you want to save when the user makes any changes to a specific post. You suggested checking every x seconds for changes, but that's not ideal (although it would be easy to implement with setInterval

). You also mentioned saving changes on page exits, but there is no guarantee that something will happen when the page exits (for example, the user will lose power).



To avoid the above, I fire up the ajax call when the user clicks the "like" button, but throttles them after the first click and saves their changes while the throttle timer is running, and immediately pops all their changes after the timer ends.

Here's what my plunker code does in a nutshell:

  • The user "likes" or "dislikes" the message, and he will make an Ajax call to the server with the new information in the message. At this point, any new "nice" / "unlike" are put into the "queue" of messages that need to be updated.

  • When the first Ajax call is successful, the throttle timer will start. In the example I provided, this is 5 seconds. Any changes in the message ("likes", "unlike") will be thrown into the same "queue".

  • After 5 seconds, it will check the "queue". If it is empty, no action is taken. If it has elements in it (like messages that have changed), it will make a second ajax call and update the messages on the server.

My example will not reflect what you are working on, but it does matter. You can change the code so that it doesn't throttle for such a long time, or it only throttles after x number of ajax calls in a certain time, etc.

+5


source







All Articles