DS.Adapter#shouldReloadAll()

shouldReloadAll (store, snapshotRecordArray) Boolean

Defined in addon/adapter.js:527
Available since 1.13.0

This method is used by the store to determine if the store should reload all records from the adapter when records are requested by store.findAll.

If this method returns true, the store will re-fetch all records from the adapter. If this method returns false, the store will resolve immediately using the cached records.

For example, if you are building an events ticketing system, in which users can only reserve tickets for 20 minutes at a time, and want to ensure that in each route you have data that is no more than 20 minutes old you could write:

shouldReloadAll: function(store, snapshotArray) {
  var snapshots = snapshotArray.snapshots();

  return snapshots.any(function(ticketSnapshot) {
    var timeDiff = moment().diff(ticketSnapshot.attr('lastAccessedAt'), 'minutes');
    if (timeDiff > 20) {
      return true;
    } else {
      return false;
    }
  });
}

This method would ensure that whenever you do store.findAll('ticket') you will always get a list of tickets that are no more than 20 minutes old. In case a cached version is more than 20 minutes old, findAll will not resolve until you fetched the latest versions.

By default this methods returns true if the passed snapshotRecordArray is empty (meaning that there are no records locally available yet), otherwise it returns false.

Note that, with default settings, shouldBackgroundReloadAll will always re-fetch all the records in the background even if shouldReloadAll returns false. You can override shouldBackgroundReloadAll if this does not suit your use case.

Parameters:

store DS.Store
snapshotRecordArray DS.SnapshotRecordArray

Returns:

Boolean
doc_EmberJs
2016-11-30 16:49:26
Comments
Leave a Comment

Please login to continue.