Results 1 to 7 of 7

Thread: redirectTo route on proxy store onRestException

  1. #1
    Sencha User
    Join Date
    Dec 2008
    Location
    France
    Posts
    321
    Answers
    5

    Default redirectTo route on proxy store onRestException

    I've a route to a login form defined in the app controller.
    I've a store with a proxy.
    When there's is RestException for the proxy, i ask the user if he wants to connect again. So i need to redirectTo the login form.

    Code:
    Ext.define('MY_APP.controller.Routes', {
        extend: 'Ext.app.Controller',
    
        requires: [
            'Ext.route.Route',
            'Ext.util.History',
            'MY_APP.Global'
        ],
    
        listen: {
            controller: {
                '#': {
                    unmatchedroute: 'onUnmatchedRoute'
                }
            }
        },
    
        routes: {
            'login': {
                name: 'login',
                action: 'onLogin'
            }
        },
        onLogin: function() {
            Ext.widget('auth.login');
        }
    });
    Code:
        onRestException: function(proxy, response, operation, eOpts) {
            var reponse = Ext.JSON.decode(response.responseText);
                deconnexion = reponse.deconnexion;
                message = reponse.message;
            if (deconnexion === 0) {
                Ext.MessageBox.show({
                    title: 'Error',
                    msg: message,
                    icon: Ext.MessageBox.ERROR,
                    buttons: Ext.Msg.OK
                });
            } else {
                Ext.Msg.show({
                    buttons: Ext.Msg.YESNO,
                    buttonText : {
                        'yes': 'I login again',
                        'no': 'I will do it later'
                    },
                    closable: false,
                    fn: function (button, text) {
                        if (button == 'yes') {
                            this.redirectTo('login',false); <== Here, how to call the login route ?
                        } else if (button == 'no') {
                            return '';
                        }
                    },
                    icon: Ext.Msg.INFO,
                    message:  operation.getError(),
                    title: "Déconnexion"
                });
            }
        }
        }

  2. #2
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,379
    Answers
    3997

    Default

    A simple case of scope, you need to either 1) cache the controller scope to a variable outside your fn function or 2) pass a scope to the Ext.Msg.show call.

    1):

    Code:
    onRestException: function(proxy, response, operation, eOpts) {
            var me = this,  // <------- scope cached to me variable
                reponse = Ext.JSON.decode(response.responseText);
                deconnexion = reponse.deconnexion;
                message = reponse.message;
            if (deconnexion === 0) {
                Ext.MessageBox.show({
                    title: 'Error',
                    msg: message,
                    icon: Ext.MessageBox.ERROR,
                    buttons: Ext.Msg.OK
                });
            } else {
                Ext.Msg.show({
                    buttons: Ext.Msg.YESNO,
                    buttonText : {
                        'yes': 'I login again',
                        'no': 'I will do it later'
                    },
                    closable: false,
                    fn: function (button, text) {
                        if (button == 'yes') {
                            me.redirectTo('login',false); // <----- me variable from before used
                        } else if (button == 'no') {
                            return '';
                        }
                    },
                    icon: Ext.Msg.INFO,
                    message:  operation.getError(),
                    title: "Déconnexion"
                });
            }
        }
        }
    2)

    Code:
    onRestException: function(proxy, response, operation, eOpts) {
            var reponse = Ext.JSON.decode(response.responseText);
                deconnexion = reponse.deconnexion;
                message = reponse.message;
            if (deconnexion === 0) {
                Ext.MessageBox.show({
                    title: 'Error',
                    msg: message,
                    icon: Ext.MessageBox.ERROR,
                    buttons: Ext.Msg.OK
                });
            } else {
                Ext.Msg.show({
                    buttons: Ext.Msg.YESNO,
                    buttonText : {
                        'yes': 'I login again',
                        'no': 'I will do it later'
                    },
                    closable: false,
                    fn: function (button, text) {
                        if (button == 'yes') {
                            this.redirectTo('login',false);
                        } else if (button == 'no') {
                            return '';
                        }
                    },
                    icon: Ext.Msg.INFO,
                    message:  operation.getError(),
                    title: "Déconnexion",
                    scope: this // <--- this tells the fn function what scope to execute with
                });
            }
        }
        }
    Mitchell Simoens @LikelyMitch

    Check out my GitHub:
    https://github.com/mitchellsimoens

    Posts are my own, not any current, past or future employer's.

  3. #3
    Sencha User
    Join Date
    Dec 2008
    Location
    France
    Posts
    321
    Answers
    5

    Default

    Hello,

    In both case, there's an error message :
    Code:
    TypeError: me.redirectTo is not a function
    
    TypeError: this.redirectTo is not a function
    When i debug under Firefox, the "me" or the "this" is the store.

    Here are more explanations to properly situate my case.

    I've a menu.
    When i click on an item, i show a grid and i load the grid's store.
    It's on the load of this grid's store that i can have the restexception and redirect to the login route.


    I read the doc and i understand that the route is linked with controller.
    As there is no controller for a store, it would be with that of the grid ?
    But then we can not go through the store restException ?
    It's still confusing for me.

  4. #4
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,379
    Answers
    3997

    Default

    Oh, is that function not in a controller? I assumed it was since a controller is only where the redirectTo method is at.
    Mitchell Simoens @LikelyMitch

    Check out my GitHub:
    https://github.com/mitchellsimoens

    Posts are my own, not any current, past or future employer's.

  5. #5
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,379
    Answers
    3997

    Default

    In your view, add the listener for exception event on the store do that it can be properly resolved in a controller. Logic shouldn’t go in a store anyway.
    Mitchell Simoens @LikelyMitch

    Check out my GitHub:
    https://github.com/mitchellsimoens

    Posts are my own, not any current, past or future employer's.

  6. #6
    Sencha User
    Join Date
    Dec 2008
    Location
    France
    Posts
    321
    Answers
    5

    Default

    Quote Originally Posted by mitchellsimoens View Post
    Oh, is that function not in a controller?
    No, it's the onRestException function of the proxy of the store.
    Quote Originally Posted by mitchellsimoens View Post
    I assumed it was since a controller is only where the redirectTo method is at.
    That's why I gave additional explanations, so that there is no confusion

  7. #7
    Sencha User
    Join Date
    Dec 2008
    Location
    France
    Posts
    321
    Answers
    5

    Default

    Quote Originally Posted by mitchellsimoens View Post
    In your view, add the listener for exception event on the store do that it can be properly resolved in a controller.
    I've tried it, but my app is little more complicated.

    I build a dynamic menu in a view.
    Code:
    createMenuItem: function(record) {
                            menuItem = Ext.create('Ext.menu.Item', {
                            text: record.get('text'),
                            glyph: glyph,
                            iconCls: iconCls,
                            qtip: qtip,
                            qtitle: qtitle,
                            record: record,
                           handler: this.affiche_fonction.bind(this, xtype, record, iconCls)
                        });
    }
    
    affiche_fonction: function(xtype, record, iconCls) {
                          fonctionnalite.store.load({
                                                                   <== try to call the controller function here, but not working
                         })
    }
    Finally, after some different tests, I found this solution, keeping the exception of the store :
    Code:
        onRestException: function(proxy, response, operation, eOpts) {
            var reponse = Ext.JSON.decode(response.responseText);
                deconnexion = reponse.deconnexion;
                message = reponse.message;
            if (deconnexion === 0) {
                Ext.MessageBox.show({
                    title: 'Error',
                    msg: message,
                    icon: Ext.MessageBox.ERROR,
                    buttons: Ext.Msg.OK
                });
            } else {
                Ext.Msg.show({
                    buttons: Ext.Msg.YESNO,
                    buttonText : {
                        'yes': 'I login again',
                        'no': 'I will do it later'
                    },
                    closable: false,
                    fn: function (button, text) {
                        if (button == 'yes') {
                            MY_APP.app.getController('Routes').onLogin(); <== Here, i call the route function directly, not the route itself
                        } else if (button == 'no') {
                            return '';
                        }
                    },
                    icon: Ext.Msg.INFO,
                    message:  operation.getError(),
                    title: "Disconnected"
                });
            }
        }
        }

    Quote Originally Posted by mitchellsimoens View Post
    Logic shouldn’t go in a store anyway.
    So, good practices would be to only display messages on the stores exception ?

Similar Threads

  1. Replies: 2
    Last Post: 4 Jul 2017, 7:11 PM
  2. Replies: 2
    Last Post: 10 Mar 2017, 4:47 PM
  3. after calling redirectTo route execute twice
    By jamesdesouza in forum Ext JS 6.x Q&A
    Replies: 3
    Last Post: 15 Jan 2017, 5:25 AM
  4. Replies: 1
    Last Post: 2 Sep 2015, 11:27 AM
  5. Route method called twice when using app.redirectTo()
    By midori_green in forum Sencha Touch 2.x: Q&A
    Replies: 3
    Last Post: 21 Jun 2013, 2:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •