27

I'm using the new router and ember data rev 11.

I have a need to force ember-data to re-load data for a record from the server. Using App.MyRecord.find(2) in setInterval function loads the data from the client local store.

How can I reload the data from the server?

Mike Aski
  • 8,960
  • 4
  • 41
  • 59
ramigg
  • 1,229
  • 1
  • 15
  • 16
  • Are you looking for this? http://stackoverflow.com/questions/10563384/how-to-refresh-entity-using-ember-data?rq=1 – adivis Jan 06 '13 at 10:30
  • Yes, I looked at it and many other places, including internal API. It doesn't work. My Model is: App.Channel.find('ch_66'). The request to the server is /channels/ch_66 which is SHOW request (get specific channel with id='ch_66'). When using findQuery('ch_66') requests the server /channels?ch_66 which is INDEX request (get all channels) – ramigg Jan 06 '13 at 10:59

1 Answers1

55

I just pushed record.reload() to Ember Data. This will ask the adapter to reload the data from the server and update the record with the new data.

Constraints:

  • You can only call reload() on a record if it has completed loading and has not yet been modified. Otherwise, the returned data will conflict with the modified data. In the future, we will add support for a merge hook to address these sorts of conflicts, which will allow reload() in more record states.
  • If you call reload() and change or save the record before the adapter returns the new data, you will get an error for the same reason. The error currently looks something like Attempted to handle event 'reloadRecord' on <Person:ember263:1> while in state rootState.loaded.updated.uncommitted.. Basically, this means that your record was in the "updated but uncommitted" state, and you aren't allowed to call reload() outside of the "loading and unmodified" state.
Yehuda Katz
  • 27,905
  • 12
  • 85
  • 91
  • Curious, does the state of the record change while reload() is loading? I forget if there's a state when the object is initially loading that prevents it from being modified...if so could it use a similar substate in the reload() case that would obviate constraint #2? – S'pht'Kr Jan 07 '13 at 09:21
  • 7
    Yehuda...this seems to be refreshing the record, but it's not updating the UI like I would expect. Is there something that needs to be called to apply changes to the record to the UI? – commadelimited Mar 05 '13 at 21:55
  • @commadelimited If your record is dirty, the view will not rerender. A [get()](https://github.com/emberjs/data/blob/v1.0.0-beta.6/packages/ember-data/lib/system/model/attributes.js#L221) will return the locally modified value. You can however do a `yourRecord.reload().then(function(record) { record.rollback() })` – Valer Feb 25 '14 at 17:34
  • 2
    is there any news on possibility of a `merge` option for reload? or giving override priority to either the local or remote version of the model? – alternated direction Apr 16 '15 at 21:18