1. #1
    Sencha User
    Join Date
    May 2012
    Posts
    63
    Vote Rating
    6
    jeffrey.hobson is on a distinguished road

      0  

    Default Answered: Store occasionally loads too early OR models loading too late

    Answered: Store occasionally loads too early OR models loading too late


    Hi all,
    I have a store which is set to autoLoad whose model contains the proxy. This proxy uses a static variable that I declare in another model to define the URL. About 80% of the time everything works fine, however, 20% of the time the app produces an error saying either that the model containing the variables (BaseModel) is undefined or that the model namespace (MyApp.model) is undefined. Refreshing my browser will usually get it to work again, but I can't have the app hang at a white screen 20% of launches.

    I'm using Sencha Touch 2.0.0.
    I've tested in Chrome, Safari (Desktop), Safari (iPhone), and Safari (iPad), but not native packaging.
    Specific errors I'm receiving:
    "Uncaught TypeError: Cannot read property 'VAR1_NAME' of undefined",
    "Uncaught TypeError: Cannot read property 'BaseModel' of undefined",

    Example code:

    BaseModel.js:
    Code:
    /**
     * File: app/model/BaseModel.js
     * 
     * Base model for all other models to extend. Overrides the save function.
     */
    
    
    Ext.define('MyApp.model.BaseModel', {
        extend: 'Ext.data.Model',
        
        statics: {
            VAR1_NAME: 'https://myurl.com/link1',
            VAR2_NAME: 'https://myurl.com/link2',
            VAR3_NAME: 'https://myurl.com/link3',
            VAR4_NAME: 'https://myurl.com/link4',
        },
        // config and other functions below ...
    });
    Primary Model:
    Code:
    /**
     * File: app/model/PrimaryModel.js
     */
    
    
    Ext.define('MyApp.model.PrimaryModel', {
        extend: 'MyApp.model.BaseModel',
        
        config: {
            // fields and other configs here ...
            proxy: {
                type: 'myCustomType',
                url: MyApp.model.BaseModel.VAR1_NAME
            }
        }
    });
    Again, I only receive the error about 20% of the time; the rest of the time everything works properly, so I'm fairly certain it's not how I'm storing the static variables or anything. In my app I have several models that use the same variables, so I can't just move them to the PrimaryModel, unfortunately. If someone has a solution or a workaround, I'd love to hear it.
    --
    Jeff

  2. Instead of having a BaseModel.js, couldn't you include those statics right in your app.js?

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,549
    Vote Rating
    873
    Answers
    3567
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Is the BaseModeal loaded when the store's JS file is loaded? Believe you have a racing condition
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    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 in print!

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

  4. #3
    Sencha User
    Join Date
    May 2012
    Posts
    63
    Vote Rating
    6
    jeffrey.hobson is on a distinguished road

      0  

    Default


    Quote Originally Posted by mitchellsimoens View Post
    Is the BaseModeal loaded when the store's JS file is loaded? Believe you have a racing condition
    The BaseModel is loaded in app.js (listed as the first model) and, if my understanding of the load order is correct, should be loaded before the store anyways since the store uses PrimaryModel and PrimaryModel extends BaseModel. Or does extend not force the parent class to be loaded first?

  5. #4
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,549
    Vote Rating
    873
    Answers
    3567
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Ok but the browser will execute app.js and if the model has not been downloaded and evaluated then the static variable will not exist.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    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 in print!

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

  6. #5
    Sencha User
    Join Date
    May 2012
    Posts
    63
    Vote Rating
    6
    jeffrey.hobson is on a distinguished road

      0  

    Default


    Are the files loaded asynchronously by app.js? If not, then all the models should be downloaded before the store tries to load, right? Then the static variable would exist in time.

  7. #6
    Sencha User
    Join Date
    May 2012
    Posts
    63
    Vote Rating
    6
    jeffrey.hobson is on a distinguished road

      0  

    Default


    Quote Originally Posted by mitchellsimoens View Post
    Ok but the browser will execute app.js and if the model has not been downloaded and evaluated then the static variable will not exist.
    I went and double-checked in the network trace; BaseModel.js is getting loaded before the error is being thrown.

  8. #7
    Sencha User jerome76's Avatar
    Join Date
    Apr 2012
    Location
    New Jersey
    Posts
    1,149
    Vote Rating
    99
    Answers
    132
    jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice

      1  

    Default


    Instead of having a BaseModel.js, couldn't you include those statics right in your app.js?

  9. #8
    Sencha User
    Join Date
    May 2012
    Posts
    63
    Vote Rating
    6
    jeffrey.hobson is on a distinguished road

      0  

    Default


    I was not aware of that option, thank you! I'll try that and see how it goes. Regardless, I do believe that it should‚Äč work the other way as well and is probably a Sencha bug that the files aren't handled in the correct order.

  10. #9
    Sencha User
    Join Date
    May 2012
    Posts
    63
    Vote Rating
    6
    jeffrey.hobson is on a distinguished road

      0  

    Default


    Quote Originally Posted by jerome76 View Post
    Instead of having a BaseModel.js, couldn't you include those statics right in your app.js?
    How would you do this?
    I tried adding the "statics" block to the "Ext.application" portion of my app.js, but when attempting to access them using "MyApp.VAR1_NAME", I receive errors. Should I be placing the statics elsewhere in app.js, or calling them differently?

    Edit: I discovered the correct way to call them was actually "MyApp.app.statics.VAR1_NAME". Thanks for all your help!

  11. #10
    Sencha User jerome76's Avatar
    Join Date
    Apr 2012
    Location
    New Jersey
    Posts
    1,149
    Vote Rating
    99
    Answers
    132
    jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice

      1  

    Default


    This is how I do it in my App.js just in case:
    Code:
    Ext.application({
        name: 'App',
        launch: function(){
            ...
        }
    });
    
    
    /**
     * All static functions and variables declared here
     * can be accessed in any JS by using
     * App.[varName | funcName()]
     * (i.e App.mask([container], 'Masked...'))
    App = {
        loggedIn: false,
        mask: function(container, text){
            container.setMasked({
                xtype: 'loadmask',
                message: text
            });
        },
        unMask: function(container){
            container.unmask();
        },
        get: function(itemId){
            return Ext.ComponentQuery.query('#' + itemId)[0];
        },
        getAll: function(xtype){
            return Ext.ComponentQuery.query(xtype);
        }
    };