Hi,
Im using the donejs supermodel generator to connect to my API. This is the generated supermodel:
....
export const documentConnection = superMap({
url: '/api/document',
idProp: 'id',
Map: Document,
List: Document.List,
name: 'document'
});
tag('document-model', documentConnection);
export default Document;
And the API is returning the data in the following format:
{
data: []
total: 395,
skip: 0,
limit: 10
}
Where data holds the rows and total represents the total number of rows before limiting the results. The problem is that sometimes, randomly, total is undefined. Im printing a table with a summary information at the footer indicating the current page and total numbers of rows, with the options to go to the previous or next page. The view:
{{#with documentList.value}}
<span><i>{{{getPaginationSummary}}}</i></span>
<div class="input-group input-group-sm pull-right" style="width:205px;">
<div class="input-group-btn">
<!--<button type="button" class="btn btn-default"><i class="fa fa-fast-backward"></i></button>-->
<button ($click)="prevPage(skip,limit)" type="button" class="btn btn-default"><i class="fa fa-backward"></i></button>
</div>
<input type="text" class="form-control text-center" value="{{{getCurrentPage}}}">
<div class="input-group-btn">
<button ($click)="nextPage(skip,limit,total)" type="button" class="btn btn-default"><i class="fa fa-forward"></i></button>
</div>
</div>
{{/with}}
In the view im calling the function getPaginationSummary, which does the following:
getPaginationSummary(ctx,tree) {
let from = parseInt(this.attr('skip')+1);
let to = parseInt(this.attr('skip')+this.attr('limit'));
let total = tree._context().attr('total');
to = (to>total?total:to);
return "Showing "+from+" to "+to+" from "+total;
},
And everything works fine, BUT sometimes randomly, when i hit to an already cached request, for example click on next page and click again to previous page, the value of total is undefined, but the API is always returning this value. So, i took a look to the browser Local Storage at the inspector and i saw all cached queries, but without the extra information like total, skip and limit, just the content of data.
I think this value is undefined because the query is cached and the app is using the cached data set. To test this, i added an extra param called timestamp with the current timestamp on each query, forcing the app to not use the cache. Now total is always defined. But with this “fix” the app is never using the cache.
Another test i did was add the function parseListData to the super model and found out that when the total was undefined the view was rendered before parseListData was executed. I could prove that with simple console.log in the parseListData function and in getPaginationSummary.
Should i create a custom model, with custom behavior to accomplish this instead using the supermodel generator? Please, any help or directions are really appreciated, thanks!!