Search code examples
sencha-touchextjs4extjsdatastore

ExtJS4 / Sencha Touch 2: Reuse data (loaded into store) in different views with different sorting


I have data in an external database which can be accessed via JSON. Now I want to provide different views on these data sets (eg. different sorting, ..). Is it possible to use one single store for this or do I need two stores accessing the same URL except with a different sorter?

My example code looks like this:

Ext.define('MyApp.store.MyStore', {
extend: 'Ext.data.Store',
requires: [
    'MyApp.model.MyModel'
],

config: {
    autoLoad: true,
    model: 'MyApp.model.MyModel',
    storeId: 'MyJsonStore',
    proxy: {
        type: 'ajax',
        url: 'http://localhost/index.php?data=MyData&format=json',
        reader: {
            type: 'json'
        }
    },
    sorters: [
        {
            direction: 'DESC',
            property: 'MyRating'
        },
        {
            direction: 'ASC',
            property: 'MyLabel'
        }
    ] } });

And one view should now render a list sorted by the rating and a second one should display a list sorted by the label.

Is there a way to prevent querying the DB twice?

Thanks - I just started out with Sencha Touch and ExtJS --- therefore please excuse my simple question ;)
Somehow I couldn't find any smart solution by asking Google for this basic task..


Solution

  • You don't have to worry about anything unless your two views are presented at the same time. Just re-sort the same store locally.

    If they are presented at the same time you will have to create a copy of a store. Otherwise they will show exact same information.