Results 1 to 2 of 2

Thread: Using on() function on store messing up store.sync()

  1. #1
    Sencha User
    Join Date
    Jan 2012
    Location
    New York City
    Posts
    109
    Answers
    1

    Default Using on() function on store messing up store.sync()

    Hello,

    I have a grid panel that is associated with a store. When a user clicks a button, I want the data changed in the panel to be synched with my server. I therefore call store.sync() to do this, which sends a POST request.

    I wanted to set up some events for the store using functions defined in the grid. I do this by calling store.on() and specifying the events. However when I do this, when I call store.sync afterward, I get the following error: Reference Error: currentStore is not defined. When commenting out this on function call, all works fine.

    Why is this occuring?

    Here is a portion of my grid:

    Code:
    Ext.define('CMPDataManagementTool.view.BaseDMTView' ,{
         extend: 'Ext.grid.Panel',
         alias : 'widget.baseDmtView',
         id: 'personPreferredNamesView',
        store: 'PersonPreferredNamesStore',
         selType: 'cellmodel',
         autoScroll:'true',
         layout:'fit',
         height: 800,
         
         initComponent: function() {
             
              this.callParent(arguments);
    
                this.addAndConfigureDockedItemsSpecifics();
                
             //columns defined here, removed for simplicity sake
            },        
            
            plugins: [{
                ptype: 'cellediting',
                clicksToEdit: 1,
                pluginId:'cellEditingPlugin'
            }],
            dockedItems: [{
                xtype: 'panel',
                dmtId: 'basePanel',
                dock: 'top',
                ui: 'footer',
                layout: 'absolute',
                style: {
                    postition: 'relative'
                },
                items: [
                        
                    { xtype: 'horizontalSeparatorPanel', itemId: 'hrTag', width: '100%', y: 155},  
                    
                    { xtype: 'button', text: 'Submit Changes', width: 100, maxWidth: 100, y:180,
                        style: {
                            right:'20px'
                        },
                  
                        initComponent: function() {
                            submitButton = this;
                        },
                        listeners: {
                            'click': function() {
                                var currentView = this.up().up();
                                var currentStore = currentView.getStore();
                               
                                
                                currentStore.on({
                                    scope: currentView,
                                    beforesync: currentView.submitStarted,
                                    write: currentView.submitFinished
                                });
                                
                                
                                currentView.getSelectionModel().deselectAll();
                        
                                currentStore.sync();
                         
                        }}},
    
    ...... // Other code not related here
    .....
    
      /**
         * Function to disable all buttons in panel
         */
        disableButtons: function(){
            submitButton.disable(true);
            clearButton.disable(true);
            this.disableSpecificButtons();
           },
           
        /**
         * Function to enable all buttons in panel
         */
           enableButtons: function(){
               submitButton.enable(true);
               clearButton.enable(true);
               this.enableSpecificButtons();
           },
           
           /**
            * Function encapsulating logic that will be executed prior to the store
            * associated with this view synching its data with the server
            */
           submitStarted: function() {
               Ext.Msg.show({
                   title:'Submitting',
                   msg: 'Changes are being submitted',
                wait:true,
                closable:false
               });
               
               var modifiedRecords = currentStore.getModifiedRecords();
               for (var i = 0; i < modifiedRecords.length; i++) {
                   var modifiedRecord = modifiedRecords[i];
                   alert(modifiedRecord.get('nlsLongDesc'))
                   modifiedRecord.set('sentToGS', true);
               }
           },
           
           /**
            * Function encapsulating the message that should be shown to users after
            * a submit button response comes back from server successfully
            */
           submitFinished: function() {
               var messageBox = Ext.Msg.show({
                   title:'DMT Response from Golden Source',
                   msg: 'Changes have been successfully sent to Golden Source',
                   animateTarget: submitButton,
                   icon: Ext.Msg.INFO,
                   closable: false
               });
    
               setTimeout(function(){
                   messageBox.close();
               }, 2700);
    
               this.enableButtons();
           },
           
         /**
            * Function encapsulating the message that should be shown to users after
            * a submit button response comes back from server with a failure :(
            */
           showSubmitError: function() {
               var errorMessageBox = Ext.Msg.show({
                   title:'DMT Response from Golden Source',
                   msg: 'An error has occured while sending changes Golden Source. Please contact support or try again.',
                   animateTarget: submitButton,
                   icon: Ext.Msg.ERROR,
                   closable: false
               });
    
               setTimeout(function(){
                   errorMessageBox.close();
               }, 2700);
               
               this.enableButtons();
           },
           
           /**
            * Removes all current records that are in this grid as well as removing
            * the current selection that is specified (to avoid javascript errors) 
            */
           removeAllRecordsFromGrid: function () {
               var orgStatisticsStore = this.getStore();
            var orgStatisticsProxy = orgStatisticsStore.getProxy();
            
               //orgStatisticsStore.removeAll(false);
            orgStatisticsStore.remove(orgStatisticsStore.getRange())
            this.getSelectionModel().deselectAll();
           },
    When the bolded italics piece is commented out all works well. When it is there, the bold line throws an undefined error. Why?

    Just for reference here is my store definition too

    Code:
    Ext.define('CMPDataManagementTool.store.PersonPreferredNamesStore', {
        extend: 'Ext.data.Store',
        model: 'CMPDataManagementTool.model.PersonPreferredNamesModel',
        alias : 'widget.PersonPreferredNames',
        storeId: 'personPreferredNamesStore',
    
        proxy: {
            type: 'ajax',
            url: '/CMPDataManagementTool/submit.html',
            reader: {
                type: 'json',
                root: 'data'
            },
            writer: {
                type: 'json',
                root: 'data',
                encode: true,
                writeAllFields: true
            },
            extraParams: {'processorType': 'personPreferredNames', 'currentUserName': 'abc'  /*getUrlFunction()["userId"]*/}
        }
    });
    Thanks,

    infernoz

  2. #2
    Sencha User
    Join Date
    Jan 2012
    Location
    New York City
    Posts
    109
    Answers
    1

    Default

    This was an error on my part. My function submitStarted() references the variable currentStore, but it is not in scope and is undefined in that particular method. I called this.getStore() and assigned that to the currentStore local variable for this to work.

    Please ignore this post

    Thanks,

    infernoz

Tags for this Thread

Posting Permissions

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