peekRecord() is not working but peekAll() is working

牧云@^-^@ 提交于 2020-01-04 05:29:09

问题


My backend always responds with all available data and it took a considerably amount of time. So I'm reloading store periodically and I plan to use peekAll() and peekRecord().

My code is:

model: function() {
  return Ember.RSVP.hash({
    'clusters': this.store.peekAll('cluster'),
    'single': this.store.peekRecord('cluster', 'cluster::My')
});

When code is executed, at first I can see that both of these items do not contain content. After few seconds data are loaded to store and I can see content 'clusters' on template as expected. But 'single' is still completely without content ({{model.single}} does not return nothing in template). But when I have a button with action:

alert(this.store.peekRecord('cluster', 'cluster::My'));

I can see that the record was found. Records are also available via Ember Inspector. What am I doing wrong that only peekAll() works in model for me.


回答1:


The semantics of both methods are:

  • store.peekAll returns a live array that is updated as the store is updated.
  • store.peekRecord returns the corresponding object in the current cache, or null, and it does not update.

So the behaviour you're observing is the expected one. If you want to use the peek methods, my advise is to make sure that the initial request has finished loading before fetching any data from the store.



来源:https://stackoverflow.com/questions/37593648/peekrecord-is-not-working-but-peekall-is-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!