Note that there are some explanatory texts on larger screens.

plurals
  1. POBreezejs - pattern for querying local cache
    primarykey
    data
    text
    <p>I have a class called Item that is filtered by PeriodId. There are many periods - but we only need to look at one at a time. I want to present the user with an initial data load (say where PeriodId==1). I then want the user to be able to query/filter on other periods. </p> <p>If the user selects PeriodId==2, I want the entityManager to query the local cache, and if the data is there, return that data. If it's not in the local cache, I want it to query the server, and add the set where PeriodId==2 to the cache. If the user then clicks on PeriodId==1, that data should already be in the cache and not round-trip to the server. </p> <p>Using the code below, I am hitting the server every time I select a period (even if I just toggle back and forth). Is there a pattern that solves this... I do NOT know the primary keys here.</p> <pre><code>var getItems = function (myObservable, periodId, forceRemote) { var pId = periodId ? periodId : 1; var query = entityQuery.from('Item') .orderBy(orderBy.items) .where("PeriodId", "==", pId); if (!forceRemote) { var r = getLocal(query); if (r) { if (r.length &gt; 3) { myObservable(r); return Q.resolve(); } } } return manager.executeQuery(query) .then(querySucceeded) .fail(queryFailed); function querySucceeded(data) { if (myObservable) { myObservable(data.results); } } }; function getLocal(query) { try { return manager.executeQueryLocally(query); } catch(e) { // set was not found in local cache, return null - forcing trip to server return null; } } </code></pre> <p><strong>UPDATE:</strong> </p> <p>Incorporating Jay's suggestion, (I had to rename 'param' to 'pId', 'q' to 'query', reset the queryParamCache if forceRemote==true, and I had to prepend the FetchStrategy with 'breeze.' ):</p> <pre><code>var queryParamCache = {}; var getItems = function(myObservable, periodId, forceRemote) { if (forceRemote) { queryParamCache = {}; } var pId = periodId ? periodId : 1; var query = entityQuery.from('Item') .orderBy(orderBy.items) .where('PeriodId', '==', pId); var isInCache = queryParamCache[pId]; if (isInCache &amp;&amp; !forceRemote) { query = query.using(breeze.FetchStrategy.FromLocalCache); } else { queryParamCache[pId] = true; query = query.using(breeze.FetchStrategy.FromServer); } return manager.executeQuery(query) .then(querySucceeded) .fail(queryFailed); function querySucceeded(data) { rosterObservable(data.results); } }; function queryFailed(error) { var msg = 'Error retreiving data. ' + error.message; logError(msg, error); throw error; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload