1. #1
    Sencha User
    Join Date
    Sep 2011
    Posts
    8
    Vote Rating
    0
    siriusblack is on a distinguished road

      0  

    Default Answered: problem with datastore change when linked with combobox

    Answered: problem with datastore change when linked with combobox


    Hi
    I have a strange problem. I have a combobox called products and a datastore called productMilestones. I need to change the content of productMilestones when the value of products combo is changed. Follwing is a code snippet of how I have loaded productMilestones:
    <code>
    var productMilestones = Ext.data.StoreManager.lookup('productMilestones');
    productMilestones.load();
    productMilestones.sort('display_order', 'ASC');
    </code>

    When I throw the output to console (i.e. console.log(productMilestones)), I can see full data, but when I try to access it from my script, e.g. productMilestones.data.length, I get 0 whilst in the console, it shows correct data, i.e. 4 in my particular case.

    Can anybody please tell me why this is happening? Thanks in advance!


    Regards,
    Sushil

  2. Try something like this:

    Code:
    addProductMilestone: function(dataStore, records) {
        var date = new Date();
    
        for (var i = 0; i < records.length; i++) {
            var record = records[i];
    
            record.set({
                assigned_company_id: this.compID,
                branch_id: 0,
                task_user_id: 0,
                updatefacility: 10,
                update_date: date
            });
    
            record.commit();
        }
    }

  3. #2
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,616
    Vote Rating
    327
    Answers
    545
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    Guessing a bit but...

    The store load is asynchronous. If you try to access the contents of the store immediately after calling load() it won't have loaded yet.

    When you dig into an object in the console it will be the current version, not necessarily the same as it was at the time you logged it out. So when it was logged it was empty but by the time you view it in the console the Ajax request will have completed and the data will be there.

    Does that fit with what you're actually doing?

    Instead of logging out the entire store try logging out a primitive property, something like this:

    Code:
    console.log(productMilestones.getCount());
    The value of a primitive can't change so there's no risk of you running into the 'liveness' problem.

  4. #3
    Sencha User
    Join Date
    Sep 2011
    Posts
    8
    Vote Rating
    0
    siriusblack is on a distinguished road

      0  

    Default Ajax takes time

    Ajax takes time


    Thanks for the reply. I understand that ajax request takes time for processing. After the load function for the store is called, I have kept some delay, and the data sent received when checking in firebug is also correct. By introducing a delay, theoretically, I think it should work, but it doesn't. My code is something like this:

    <code>
    productMilestones.proxy.url = 'test/milestones.php?productID='+selectedProduct;
    productMilestones.sort('display_order', 'ASC');
    productMilestones.load();
    /*
    load users linked with the current company
    */
    Ext.MessageBox.show({
    msg: 'Loading your data, please wait...',
    progressText: 'Loading...',
    width:300,
    wait:true,
    waitConfig: {interval:500}
    });
    setTimeout(function(){
    Ext.MessageBox.hide();
    }, 500);


    </code>

    The getCount function produces 0, so does datastore.data.length.

    Rest of processing the content of the store is done after this code. I think the delay should help, but it is not. But, when I select a different value in the combo box, it works this time.

    Its just crazy. I don't understand how and why.

  5. #4
    Sencha User Tim Toady's Avatar
    Join Date
    Feb 2010
    Location
    Pennsylvania
    Posts
    570
    Vote Rating
    181
    Answers
    62
    Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold Tim Toady is a splendid one to behold

      0  

    Default


    Use a listener for the load event and do your getCount()/processing there. You shouldn't try to guess the amount of time it takes. I don't see the getCount at all in your code (Please use code tags with brackets around your code next time so it is more readable). Also, that setTimeout isn't going to stop all JS from running. It just waits until the time has passed to call the hide function and continues working on code after the call in the mean time.

  6. #5
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,616
    Vote Rating
    327
    Answers
    545
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    CODE tags need to be surrounded by square brackets, not angle braces: [CODE]...[/CODE].

    From the code you've posted I'd agree with Tim Toady. I don't see any evidence of a meaningful delay in your code but without the console.log() it's difficult to be sure.

    An alternative to using a load event is to provide a callback to the load() method.

  7. #6
    Sencha User
    Join Date
    Sep 2011
    Posts
    8
    Vote Rating
    0
    siriusblack is on a distinguished road

      0  

    Default datastore load event

    datastore load event


    Hi All,
    Thanks for your replies. My initial idea was to populate a datastore when a value in combobox changes, and use that datastore to populate the datastore of a grid. I think this is multiple step operation, so scrapped it.

    As skirtle has said, I loaded the datastore of the grid directly when the value in combobox changes. And when the data is loaded in datastore, it calls onload event of the datastore. Here, I have filled in data in the empty columns of the datastore. The change made in datastore should be visible, but it is not. Can you help me with this?

    My code is as follows:
    Code:
        onProductChange:function(combo, value, options )
        {
            var cmbProduct = this.getComponent('fsProduct').getComponent('Product');
            var selectedProduct = combo.value;
            var gridSchedule = this.getComponent('cnSchedules').getComponent('cnScheduleList').getComponent('gridSchedule');
    
    
            if(selectedProduct > 0)
            {
                var btnAdd = this.getComponent('cnSchedules').getComponent('btnContainer').getComponent('btnAdd');
                btnAdd.enable(true);
                
                var btnReset  = this.getComponent('cnSchedules').getComponent('cnScheduleList').getComponent('btnReset');
                btnReset.enable(true);
                
                btnReset.on('click', this.resetScheduleGrid, this);
            }
    
    
            var productMilestones = Ext.data.StoreManager.lookup('productMilestoneStore');
            productMilestones.clearData();
            try
            {
                productMilestones.proxy.url = 'test/milestones.php?productID='+selectedProduct;
                productMilestones.sort('display_order', 'ASC');
                productMilestones.load();
                productMilestones.on('load', this.addProductMilestone, this);
            }
            catch(e)
            {
                console.log(e);
            }
    }
    and the function called when data is loaded in the store:

    Code:
        addProductMilestone:function(dataStore, records, successful, operation, options)
        {
            var newDate = new Date();
            for(i = 0; i < records.length; i++)
            {
                var row = records[i].data;
                row.milestone_id = row.milestone_id;
                row.name = row.name;
                row.display_order = row.display_order;
                row.assigned_company_id = this.compID;
                row.branch_id = 0;
                row.task_user_id = 0;
                row.updatefacility = 10;
                row.update_date = newDate;
            }
        }
    In the grid, only the first column is populated, the rest aren't whilst console.log shows complete data in the store.

  8. #7
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,616
    Vote Rating
    327
    Answers
    545
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    Try something like this:

    Code:
    addProductMilestone: function(dataStore, records) {
        var date = new Date();
    
        for (var i = 0; i < records.length; i++) {
            var record = records[i];
    
            record.set({
                assigned_company_id: this.compID,
                branch_id: 0,
                task_user_id: 0,
                updatefacility: 10,
                update_date: date
            });
    
            record.commit();
        }
    }

  9. #8
    Sencha User
    Join Date
    Sep 2011
    Posts
    8
    Vote Rating
    0
    siriusblack is on a distinguished road

      0  

    Default


    Thanks Skirtle! That solved my problem!

Thread Participants: 2

Tags for this Thread