Ember-data update from rest api


#1

Hi everyone, i open a discuss on http://stackoverflow.com/questions/28190412/ember-data-update-data-from-rest-api

But after some responses i have some doubts and I decided to ask here.

I did a lot of researches on google/starckoveflow/ember doc for understand how to keep my frontend updated with backend and guarantee a good user expirince(no flickering) but I didn’t find nothing that clearly explain how to achieve my needs. So my doubt is that i didn’t understand emberjs and ember-data.

How can I keep my frontend data synced periodically with my backend? Is ember-data designed for this scope?


#2

Have you already read this?

http://emberjs.com/guides/models/frequently-asked-questions/


#3

@kgish yes, but:

in model hook i use this.store.filter instead this store.find and inside onPoll function i use controller.store.find(‘mymodel’).then(function(data){_this.store.pushMany(‘mymo‌ del’, data);});. And this works in part with no flickering: new records are added and existings are updated. The problem is when a record is deleted from backend. Is there the possibility to tell to pushMany function to remove records for which there isn’t an update?

edit

I have tried to put

controller.set('model', controller.store.find('mymodel'))

in Ember.Run(http://stackoverflow.com/questions/14883553/mitigating-a-flickering-ui-when-refreshing-content-in-ember)


#4

I create a simple demo at http://emberjs.jsbin.com/jebiqijimo/1/edit When you click on refresh you see a fast blinking in template.


#5

In you refresh action:

  actions: {
    refresh: function(){
      this.store.unloadAll('Test');
      this.store.find('Test');
    }
  }

you unload all records of type ‘test’ and ask the sever to give you the entire list of ‘tests’. It’s ok but, you never use the result of this.store.find call. You need to do something with the result the server provides to your calls:

...
this.store.find('test').then(funtion(tests){
    do something with the new test record array
});

http://emberjs.com/guides/routing/specifying-a-routes-model/#toc_setting-up-controllers-with-the-model

http://emberjs.com/guides/routing/asynchronous-routing/#toc_a-word-on-promises

http://emberjs.com/api/data/classes/DS.Store.html#method_find


#6

@nandosan something like this:

`refresh: function(){
  var _this = this;
  this.store.unloadAll('Test');
  this.store.find('Test').then(function(data){
    _this.store.pushMany('Test', data);
  });
} `

#7

No. When you’ll be inside the success function of your fulfilled promise, your store as been already populated with the tests objects (which means received a json from the server, parsed/deserialized, created as many DS objects as needed - also related, sideloaded, ones -)

Your liveupdating objects will be auto-updated and you can do additional stuff in the promise success function.