I have an application using Breeze to request data. I want to check the local cache first and then the server cache if the results do not return (I followed the Jumpstart course from John Papa SPA). However, I found a flaw in my logic, and I'm not sure how to fix it. Assuming I have 10 elements that match my query.
Situation 1 (which works): I go to the list page (Page A) that displays all 10. The hit server as a cache is empty and adds all 10 to the cache. Then go to the page displaying 1 result (Page B), which is in the cache. So all is well.
Situation 2 (problem): I go to a page displaying 1 record first (Page B). Then I go to the list page (Page A), which checks the cache and finds 1 entry, and because of this line ( if (recordsInCache.length > 0) ) it goes out and displays only one entry.
For some reason I need to know that on the server (9) there are no more entries that are NOT in the cache, i.e. the general records for this request are actually 10, I have 1, so I have to hit the server for the remaining 9.
Here is my request for page A:
function getDaresToUser(daresObservable, criteria, forceServerCall) { var query = EntityQuery.from('Dares') .where('statusId', '!=', enums.dareStatus.Deleted) .where('toUserId', '==', criteria.userId) .expand("fromUser, toUser") .orderBy('deadlineDate, changedDate'); return dataServiceHelper.executeQuery(query, daresObservable, false, forceServerCall); }
and here is my request for page B (separate element)
function getDare(dareObservable, criteria, forceServerCall) { var query = EntityQuery.from('Dares') .expand("fromUser, toUser") .where('dareId', '==', criteria.dareId); return dataServiceHelper.executeQuery(query, dareObservable, true, forceServerCall); } function executeQuery(query, itemsObservable, singleEntity, forceServerCall) { //check local cache first if (!manager.metadataStore.isEmpty() && !forceServerCall) { var recordsInCache = executeLocalQuery(query, itemsObservable, singleEntity); if (recordsInCache.length > 0) { callCompleted(); return Q.resolve(); } } return manager.executeQuery(query) .then(querySucceeded) .fail(queryFailed);
}
function executeLocalQuery(query, itemsObservable, singleEntity) { var recordsInCache = manager.executeQueryLocally(query); if (recordsInCache.length > 0) { processQueryResults(recordsInCache, itemsObservable, singleEntity, true); } return recordsInCache; }
Any advice appreciated ...