javascript - Dojo Memory Store Periodic Refresh -


i have dojo method make xhrrequest latest list of items db , periodically using setinterval(). there other better way without using setinterval() automatically update memory store whenever new item added db?.. current code below

<div data-dojo-type="dojo/store/memory"              data-dojo-id="datastore">             <script type="dojo/method">   var mystore = this; setinterval(function(){  require(["dojo/request/xhr"], function(xhr){   xhr("myurl", {     handleas: "json"   }).then(function(data){ if (data && data.length > 0) {                            mystore.setdata(data);                         }   }, function(err){     // handle error condition   }, function(evt){     // handle progress event request if     // browser supports xhr2   }); }); }, 2000);                               </script>         </div> 

well, use dojo/store/jsonrest in stead of dojo/store/memory. if need information date, should call "myurl" each time want fetch data, don't have poll server changes.

if don't have restful api, , you're not willing implement own dojo/store store (which possibility well), might want websockets.

the problem approach it's quite network consuming (certainly if there many clients). make lot more sense if server push messages client, saying database changed , have change store.

this similar approach how platforms meteor work, sync serverside databases (partly) clientside stores using websockets.

however, not browsers support websockets (caniuse.com), frameworks provide server+clientside component can use same fallback (long polling example).

take @ real-time stores article, explains bit further detail.


Popular posts from this blog