Sunday, 15 April 2012

javascript - Save only changed objects on server : Restful service + angularjs -



javascript - Save only changed objects on server : Restful service + angularjs -

i display list of posts on page button user can toggle on or off. in order throttle traffic server, to:

send updates all posts status has changed @ once in same request send updates modified posts only update status each n seconds, , on page exit only. not each time user toggles button.

is possible angularjs?

i've written illustration can seen here:

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

i've spent time on commenting, , providing useful info gets displayed on page when runs. alter around delays if you'd run slower if messages moving quickly.

from question looks want save when user makes changes particular post. proposed checking every x seconds changes, isn't ideal (though simple implement setinterval). mentioned saving changes on page exit, it's impossible guarantee happens on page exit (a user loosing powerfulness example).

to avoid above, fire ajax phone call when user clicks "like" button, throttle them after first click & store changes while throttle timer running , force changes @ 1 time after timer ends.

here plunker code in nutshell:

user "likes" or "unlikes" post , create ajax phone call server new info on post. @ point, new "likes" / "unlikes" gets thrown "queue" of posts need updated.

when first ajax phone call successful, throttle timer starts. in illustration i've provided 5 seconds. changes post ("likes", "unlikes") thrown in same "queue".

after 5 seconds up, check "queue". if it's empty, no action taken. if has items in (e.g. posts have changed), create sec ajax phone call , update posts on server.

my illustration won't mirror you're working on exactly, it's concept matters. modify code doesn't throttle such long time, or have throttle after x number of ajax calls in amount of time, etc.

javascript angularjs

No comments:

Post a Comment