Extjs4: How to share data between multiple stores or models?

后端 未结 1 1350
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 00:37

I\'m new to Ext and I\'m struggling to figure out Models Stores and Proxies.

The server returns one large JSON object. For example.

{ 
    \"respon         


        
相关标签:
1条回答
  • 2020-12-09 00:39

    You are pretty much there, and although I'm pretty sure you are understanding it all, for the benefit of others allow me to give an extended answer and a slightly modified solution to your problem.

    Definitions:

    • The Model - primarily defines the fields a record has.
    • A Store - holds a collection of records.
    • A Proxy - facilitates server communication through a chosen method (Ajax, Direct, etc.) and maps CRUD (Create/Read/Update/Destroy) operations when such result from a change in the associated store or model.
    • A Reader - Tells a proxy how to interpret the data the server returns.
    • A Grid or Combobox - can display store records.

    You scenario is not an uncommon one - while by default ExtJS loads each store separately, it is likely an application would prefer various stores to be loaded at once through a single read call; for example, when rendering one store-dependent component is dependent on another store.

    Your code is not far off from achieving this, but here is how I do it. In effect, when a 'master' (Tasks) store loads, the server response also carries the data of a 'slave' (Tags) store, which is then manually loaded to that 'slave' store.

    The 'slave' store (notice autoload: false and no read operation):

    Ext.define('DL.store.Tags', {
        extend: 'Ext.data.Store',    
        model: 'DL.model.Tag',
    
        // Notice the tags are actually returned when the tasks are loaded and loaded into this store by the TasksStore.
        autoLoad: false, 
        autoSync: true,
    
        proxy: {
            type: 'direct',
    
            api: {
                create:  Tags.Create,
                update:  Tags.Update,
                destroy: Tags.Destroy,
            },
    
            reader: {
                type: 'json',
                root: 'tags'
            }        
        },    
    
    });
    

    Then the 'master' store:

    Ext.define('DL.store.Tasks', {
        extend: 'Ext.data.TreeStore',    
        model: 'DL.model.Task',
    
        autoLoad: true,   
        autoSync: true,
    
        root: {
            text: 'Root',
            id: null,
            expanded: true
        },
    
        proxy: {
            type: 'direct',
    
            api: {
                create:  Tasks.Create,
                read:    Tasks.Read,
                update:  Tasks.Update,
                destroy: Tasks.Destroy,
            },
    
        },
    
        onProxyLoad: function( aOperation )
        {
            // A read request for tasks will also return the user tags.
            // So feed these tags into their store.
            var iResult = aOperation.response.result,
                iTagsStore = Ext.StoreManager.lookup( 'Tags' );
    
            if ( Ext.isDefined( iResult.tags ) )
                iTagsStore.loadRawData( iResult );
    
            // We need this line for "Tasks" store to load its own data
            this.callParent(arguments);       
        }
    });
    

    Basically all it does is it takes part of the server response and loads it manually to the 'slave' store.

    The PHP server side code (for tasks read operation) involves:

    return array(
        'success'  => true,
        'children' => $iNodes,
        'tags' => $iTags            
    );
    

    Where children is the reader's root of the 'master' store, and tags is additional data that is then loaded into the 'slave' store.

    I hope you can work how how to apply these concepts to your code.

    0 讨论(0)
提交回复
热议问题