Subclass QueryReadStore or ItemFileWriteStore to include write api and server side paging and sorting.

StackOverflow https://stackoverflow.com/questions/9922815

Question

I am using Struts 2 and want to include an editable server side paging and sorting grid.

I need to sublclass the QueryReadStore to implement the write and notification APIs. I do not want to inlcude server side REST services so i do not want to use JsonRest store. Any idea how this can be done.? What methods do i have to override and exactly how. I have gone through many examples but i am not getting how this can be done exactly.

Also is it possible to just extend the ItemFileWriteStore and just override its methods to include server side pagination? If so then which methods do i need to override. Can i get an example about how this can be done?

Was it helpful?

Solution

Answer is ofc yes :) But do you really need to subclass ItemFileWriteStore, does it not fit your needs? A short explaination of the .save() follows.

Clientside does modify / new / delete in the store and in turn those items are marked as dirty. While having dirty items, the store will keep references to those in a has, like so:

store._pending = { _deletedItems: [], _modifiedItems: [], _newItems: [] };

On call save() each of these should be looped, sending requests to server BUT, this does not happen if neither _saveEverything or _saveCustom is defined. WriteStore simply resets its client-side revert feature and saves in client-memory. See source search "save: function"

Here is my implementation of a simple writeAPI, must be modified to use without its inbuilt validation: OoCmS._storeAPI

In short, follow this boiler, given that you would have a CRUD pattern on server:

new ItemFileWriteStore( {
    url: 'path/to/c**R**ud',
    _saveCustom: function() {
        for(var i in this._pending._newItems) if(this._pending._deletedItems.hasOwnProperty(i)) {
          item = this._getItemByIdentity(i);
          dxhr.post({ url: 'path/to/**C**rud', contents: { id:i }});
        }

        for(i in this._pending._modifiedItems) if(this._pending._deletedItems.hasOwnProperty(i)) {
          item = this._getItemByIdentity(i);
          dxhr.post({ url: 'path/to/cr**U**d', contents: { id:i }});
        }

        for(i in this._pending._deletedItems) if(this._pending._deletedItems.hasOwnProperty(i)) {
          item = this._getItemByIdentity(i);
          dxhr.post({ url: 'path/to/cru**D**', contents: { id:i }});
    }
});

Now; as for paging, ItemFileWriteStore has the pagination in it from its superclass mixins.. You just need to call it with two setups, one being directly on store meaning server should only return a subset - or on a model with query capeabilities where server returns a full set.

var pageSize = 5,    // lets say 5 items pr request
    currentPage = 2; // note, starting on second page (with *one* being offset)
store.fetch({
  onComplete: function(itemsReceived) { },
  query: { foo: 'bar*' },               // optional filtering, server gets json urlencoded
  count: pageSize,                      // server gets &count=pageSize
  start: currentPage*pageSize-pageSize  // server gets &start=offsetCalculation
});

quod erat demonstrandum

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top