1. #1
    Sencha User
    Join Date
    Sep 2012
    Posts
    4
    Vote Rating
    0
    Frewland is on a distinguished road

      0  

    Default Unanswered: How to notify a view that a store has been refreshed ?

    Unanswered: How to notify a view that a store has been refreshed ?


    I am trying to make a store to notify a view whenever it's been refreshed. (For various reasons I would prefer not to use a DataView.) I thought I could do it like this:

    In the store I declare a custom event myEvent and have the refresh handler fire it on the app to make it global:

    Code:
    Ext.define('MyApp.store.MyStore', {
        extend: 'Ext.data.Store',
    
        config: {
          ...
            listeners: [
                {
                    fn: 'onDirectstoreRefresh',
                    event: 'refresh'
                },
                {
                    fn: 'onDirectstoreMyEvent,
                    event: 'myEvent'
                }
            ]
        },
    
    
        onDirectstoreRefresh: function(store, data, eOpts) {
                MyApp.app.fireEvent('myEvent', this, 0);
        },
    
        onDirectstoreMyEvent: function(eventOptions) {
                console.log('MyEvent received in MyStore');  // This bit works
        }
    });
    and then have the view listen for myEvent:

    Code:
    Ext.define('MyApp.view.root', {
        extend: 'Ext.Container',
    
        config: {
    ...
            listeners: [
    
                {
                    fn: 'onContainerMyEvent',
                    event: 'myEvent'
                }
            ]
        },
    
     
        onContainerMyEvent: function(container) {
            console.log('MyEvent received in view'); // This never happens
        }
    
    });
    myEvent is received inside the store but it never gets to the view. What have I missed out ?

    Thanks in advance.

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    33,656
    Vote Rating
    435
    Answers
    3108
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      0  

    Default


    In the view, you should add listeners to the store
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    http://www.JSONPLint.com - Source to lint your JSONP!

    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Nov 2010
    Posts
    112
    Vote Rating
    8
    Bunchofstring will become famous soon enough

      0  

    Default


    @Mitchell, Thanks for suggesting the preferred pattern. Is there a material difference between using the view's listeners config vs. using this.on('myEvent',doSomething) in an overridden constructor?

  4. #4
    Sencha User
    Join Date
    Sep 2012
    Posts
    4
    Vote Rating
    0
    Frewland is on a distinguished road

      0  

    Default


    Thanks, this is working now.

    In MyView I put
    Code:
            listeners: [
                {
                    fn: 'onContainerInitialize',
                    event: 'initialize'
                }
            ]

    and

    Code:
        onContainerInitialize: function(component, options) {
            this.store = Ext.data.StoreManager.lookup('MyStore');
            this.store.addListener('refresh', this.onMyStoreRefresh, this, {order: 'after'} );
            this.store.fireEvent('refresh');
        }
    and
    Code:
        onMyStoreRefresh: function(component) {
         console.log(store refreshed');
    ...
        }
    I'm not trying to use a global event anymore.

  5. #5
    Sencha User
    Join Date
    Apr 2010
    Location
    China
    Posts
    227
    Vote Rating
    19
    Answers
    64
    haduki will become famous soon enough

      0  

    Default


    In your op, you wrote
    Code:
    MyApp.app.fireEvent('myEvent', this, 0)
    'MyApp.app' is the instance of 'Application',not your 'view'.
    This is the reason why 'This never happens'.
    Add listener to your application object or fire event by your view is the correct way.
    I write English by translator.