DEPRECATION: The default behavior of shouldReloadAll will change in Ember Data 2.0 to always return false when there is at least one

こ雲淡風輕ζ 提交于 2019-12-22 03:35:14

问题


At the moment of this question i'm running the latest Ember and Ember Data versions.

I'm working with the DS.RESTAdapter calling for a /places this way:

this.store.findAll('place');

The model only have a name attribute name: DS.attr('string') The JSON is the following:

{
  places: [
    {
      id: 1,
      name: "San Francisco"
    },
    {
      id: 2,
      name: "Havana"
    }
  ]
}

I made the template and with the corresponding each and everything shows up and works so far but i get a deprecation warnings that tells the following:

DEPRECATION: The default behavior of shouldReloadAll will change in Ember Data 2.0 to always return false when there is at least one "destination" record in the store. If you would like to preserve the current behavior please override shouldReloadAll in your adapter:application and return true.

I don't know exactly what's the best approach to solve this warning. I'm new to ember so any further explanation will be fine. Thanks in advance.


回答1:


Set shouldReloadAll() { return true; } in your application adapter and the warning goes away and the current default behavior is the same.




回答2:


To answer @Eric Wilson question:

I will award the bounty to an answer that contains the necessary details to override the shouldReloadAll function. In other words, I can't figure out what to do with these snippets of code.

First use:

ember g adapter application

Then go to newly-generated app/adapters/application.js and replace code with:

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
  shouldReloadAll() {
    return true;
  }
});

You're done. Demo on Ember Twiddle.

Returning true from shouldReloadAll function basically instructs Ember to re-fetch models each time you call findAll method and fire request to your backend.

If you know that this data won't change, for example, for next 2 minutes then you can implement some logic in shouldReloadAll which would prevent Ember from firing unnecessary request (like check if 2 minutes passed, if not then prevent request - return false).

See DS.Adapter.shouldReloadAll method documentation for more context.




回答3:


To comply with the new default behavior, override shouldReloadAll like this:

function shouldReloadAll( store, snapshot ) {
    return !store.peekAll( snapshot.type.modelName ).length
}


来源:https://stackoverflow.com/questions/31016676/deprecation-the-default-behavior-of-shouldreloadall-will-change-in-ember-data-2

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