How to set Custom StateProvider?

  1. #1

    How to set Custom StateProvider?

    Hi,
    my question is simple: I am using Ext Net 2.5 via Razor syntax (MVC 5.0), where is the right place to set a custom StateProvider?

    Now I go in details in order to better explain the problem I a m experiecing. I need to persists the columns preferences per user on server side.

    In order to achieve the goal I developed my BF.StateProvider that extends the Ext.state.Provider providing storing/retrieiving GridPanel States from Controller via Direct Methods.
    Things don't work: debugging my BF.StateProvider I noticed that during the load, the "get" method is properly called and the configuration is properly retrieved and set it into Privider "state", but after that the "set" method containing the initial configuration is called from StateManager, overriding the previously loaded/stored configuration!

    I guessed that my StateProvider implementation was wrong. For checking it I simply replaced the BF.StateProvider code with the standard CookieProvider and I renamed it CUkieProvider. I took the code from here http://docs.sencha.com/extjs/4.1.3/s...CookieProvider. Also using this code the Provider state is properly set but once again after that the StateManager still invokes the "set" overriding the loaded configuration!

    In both cases I set the StateProvider into onDocumentReady event in this way:

    var onDocumentReady = function () {
        Ext.state.Manager.setProvider(
            Ext.create('Ext.state.CUkieProvider') // where CUkieProvider is CookieProvider code copied and renamed.
           //Ext.create('Ext.state.StateProvider') // my custom provider
           //Ext.create('Ext.state.CookieProvider') // also using the original one the state does not persists
        );
    }
    
    @X.ResourceManager().Listeners(ls => { ls.DocumentReady.Handler = "onDocumentReady();"; })
    Moreover, setting directly the standard CookieProvider in resource Manager makes persistance working (persistance based on cookie, obiously)
    @X.ResourceManager().StateProvider(StateProvider.Cookie)

    So my conclusion is that I am setting the StateProvider in a wrong way: where is the right place to set the StateProvider? Maybe that setting it into onDocuemtnReady is too late?

    Thanks
    Last edited by Daniil; Sep 22, 2014 at 1:20 PM. Reason: Please use [CODE] tags
  2. #2
    The issue I am experiencing is the same explained here, however I am not using sencha architect, obviously!
    However I am wonderind if the initialization of ResourceManager in Ext .Net compromises the store life cycle...

    http://www.sencha.com/forum/showthre...te-is-restored
  3. #3
    After the call onDocumentReady it seems that the grid is unable to retrieve the state from statemanager!
    I cannot explain the reason of this...

    @X.ResourceManager().Listeners(ls => { ls.DocumentReady.Handler = "onDocumentReady();"; })
    Attached Thumbnails Click image for larger version. 

Name:	stateId.jpg 
Views:	38 
Size:	97.2 KB 
ID:	15261  
    Last edited by Daniil; Sep 22, 2014 at 1:23 PM. Reason: Please use [CODE] tags
  4. #4
    Hi @MarzioPat,

    Maybe that setting it into onDocuemtnReady is too late?
    Yes, it might be.

    Please try this:
    <head>
        <title>Ext.Net.MVC v2 Example</title>  
        
        @X.ResourcePlaceHolder().Mode(ResourceMode.ScriptFiles)
        
        <script>
            Ext.state.Manager.setProvider(new YourCookieProvider());
        </script>
    </head>
  5. #5
    Thanks Daniil,
    your suggestion has solved the initialization issue.
    Now settings my Custom Cookie Provider (a State provider still based from cookies) the grid layout is saved and restored properly.

    However my custom "BF.StateProvider" store/retrieve the state from the Server side, using DirectMethods and Action Controller.
    It seems that, when the ResourceManager is initialized in this way, the usage of the "Ext.net.DirectMethod.request" will pass null parameters to the Controller!
    In details when the "BF.StateProvider" restoreState function is called, the DirectMethod.request is executed and the parameter passed from here to my Controller are always null!
    Instead when the DirectMethod.request is executed from the "BF.StateProvider" set function, the parameters are passed properly to the Controller.

    I guess that the problem is the calling a DirectMethod.request during ResourceManager initialization.
    Does it make sense? Do you have suggestions/workaround to solve this issue?


    Here my "BF.StateProvider" functions:
        restoreState: function () {
            debugger;
            var stateStorages = {};
            var me = this;
          
            Ext.net.DirectMethod.request({
                url: me.actionGet,
                disableCaching: true,
                async: false,
                params: {
                    baseStateId: 'pippo', //me.baseStateId
                },
                success: function (result, response, extraParams, o) {
                    debugger;
    
                    if (result != undefined) {
                        for (var i in result) {
                            stateStorages[result[i].Key] = me.decodeValue(result[i].Value);
                        }
                    }
                },
                failure: function (errorMessage) {
                    console.log('failed', arguments);
                }
            });
            
            debugger;
            return stateStorages;
        },
    
    
        persist: function (name, value) {
            debugger;
            var me = this;
    
            Ext.net.DirectMethod.request({
                url: me.actionSave,
                disableCaching: true,
                async: false,
                params: {
                    stateId: name,
                    value: me.encodeValue(value)
                },
                success: function (result, response, extraParams, o) {
                    debugger;
                },
                failure: function (errorMessage) {
                    console.log('failed', arguments);
                }
            });
        },
    I have additional information: here my View Code:

        <meta name="viewport" content="width=device-width" />
        <title></title>
    
        @X.ResourcePlaceHolder().Mode(ResourceMode.ScriptFiles)
        
        <script type="text/javascript" src="~/Content/js/BF.StateProvider.js"></script>
    
        <script>
    
            initializeStateProvider('@(Url.Action("StateGet"))',     // Action Get
                                    '@(Url.Action("StateDelete"))',  // Action Delete
                                    '@(Url.Action("StateSave"))',    // Action Save
                                    'EasyWeb.DummyList');            // Base StateId (Web.DummyList.MainGridPanel)
    
    ...
    And here the initializeStateProvider from BF.StateProvider.js:

    var initializeStateProvider = function (actionGet, actionDelete, actionSave, baseStateId) {
        
        debugger;
    
        // UNcommenting this makes baseStateId to be null Controller side
        //Ext.net.DirectMethod.request({
        //    url: actionGet,
        //    disableCaching: true,
        //    async: false,
        //    params: {
        //        baseStateId: 'pippo', //me.baseStateId
        //    },
        //    success: function (result, response, extraParams, o) {
        //        debugger;
        //    },
        //    failure: function (errorMessage) {
        //        console.log('failed', arguments);
        //    }
        //});
    
        debugger;
       
        Ext.state.Manager.setProvider(
            //new Ext.state.CookieProvider()
            //Ext.create('Ext.state.CookieProvider')
            //Ext.create('Ext.state.CUkieProvider')
            Ext.create('BF.StateProvider', {
                baseStateId: baseStateId,
                actionGet: actionGet,
                actionSave: actionSave,
                actionDelete: actionDelete
            })
        );
        
    };
    Note: Uncommenting the DirectMethod.request makes null parameter Controller side...

    Here my controller (if it helps)

            public virtual AjaxResult StateGet(string baseStateId)
            {
                AjaxResult result = new AjaxResult();
    
                IEnumerable<State> states = StateProvider.Get(baseStateId, CurrentUser.Id);
                result.Result = states;
    
                return result;
            }
  6. #6
    I guess that the problem is the calling a DirectMethod.request during ResourceManager initialization.
    Yes, you are right.

    Do you have suggestions/workaround to solve this issue?
    I can suggest the following.

    Example

    @{
        var X = Html.X(); 
    }
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>Ext.Net.MVC v2 Example</title>  
        
        @X.ResourcePlaceHolder().Mode(ResourceMode.ScriptFiles)
        
        <script>
            Ext.net.ResourceMgr.isMVC = true; // the key point
    
            Ext.net.DirectMethod.request(...);
        </script>
    </head>
    <body>
        @X.ResourceManager()
    
    </body>
    </html>
  7. #7
    Great!
    It works!!!

    Thank you very uch for supporting me,
    Marzio.

Similar Threads

  1. Replies: 1
    Last Post: Apr 10, 2013, 10:48 AM
  2. [CLOSED] How to Clear or Reset StateProvider?
    By csharpdev in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Dec 17, 2010, 6:28 AM
  3. Replies: 2
    Last Post: Apr 24, 2009, 6:07 PM
  4. TreePanel and StateProvider
    By Timothy in forum 1.x Legacy Premium Help
    Replies: 0
    Last Post: Jan 04, 2009, 1:14 AM
  5. Cookie StateProvider
    By Timothy in forum 1.x Help
    Replies: 0
    Last Post: Jun 26, 2008, 6:02 PM

Tags for this Thread

Posting Permissions