1. #1
    Sencha User Zyclops's Avatar
    Join Date
    Jul 2007
    Location
    Adelaide, South Australia
    Posts
    154
    Vote Rating
    0
    Zyclops is on a distinguished road

      0  

    Default Unanswered: How do you define static class variables so they can be used in the class?

    Unanswered: How do you define static class variables so they can be used in the class?


    I've got a number of fields that have a set list of values for the database. I'd like to maintain a single list of these for validations and just to avoid typos etc.

    Code:
    Ext.define('MyApp.Deck', {
        statics: {
            NEW_CARDS_ORDER: {
                IN_ORDER: 'in-order',
                REVERSE_ORDER: 'reverse-order',
                RANDOM_ORDER: 'random-order',
                ALPHABETICAL_ORDER: 'alphabetical_order',
                REVERSE_ALPHABETICAL_ORDER: 'reverse_alphabetical_order'
            }
    });
    Code:
    Ext.define('MyApp.Deck', {
        extend: 'Ext.data.Model',
        fields: [
          { 
              name: 'new_cards_order', 
              type: 'string', 
              defaultValue: MyApp.NEW_CARDS_ORDER.IN_ORDER
          }
       ]
    })
    The code above doesn't work because MyApp.NEW_CARDS is not defined. I've tried a number of other ways but can't get it to work.

    Any ideas?

  2. #2
    Ext JS Premium Member tvanzoelen's Avatar
    Join Date
    Apr 2008
    Location
    Groningen - Netherlands
    Posts
    1,114
    Answers
    83
    Vote Rating
    30
    tvanzoelen has a spectacular aura about tvanzoelen has a spectacular aura about tvanzoelen has a spectacular aura about

      0  

    Default


    Should you reference it with?

    Code:
    MyApp.Deck.NEW_CARDS_ORDER.IN_ORDER
    I don't see Deck in your code

  3. #3
    Sencha User Zyclops's Avatar
    Join Date
    Jul 2007
    Location
    Adelaide, South Australia
    Posts
    154
    Vote Rating
    0
    Zyclops is on a distinguished road

      0  

    Default


    Code:
    Ext.ns('Pro.LP.Deck')
    Pro.LP.Deck = {
        NEW_CARDS_ORDER: {
            IN_ORDER: 'in-order',
            REVERSE_ORDER: 'reverse-order',
            RANDOM_ORDER: 'random-order',
            ALPHABETICAL_ORDER: 'alphabetical_order',
            REVERSE_ALPHABETICAL_ORDER: 'reverse_alphabetical_order'
        },
    
        REVIEW_CARDS_ORDER: {
            LARGEST_INTERVAL: 'largest-interval',
            SMALLEST_INTERVAL: 'smallest-interval',
            RANDOM: 'RANDOM'
        }
    }
    
    
    /**
     * This is the users deck
     * @extends Object
     */
    Ext.define('Pro.LP.Deck', {
        statics: {
            NEW_CARDS_ORDER: {
                IN_ORDER: 'in-order',
                REVERSE_ORDER: 'reverse-order',
                RANDOM_ORDER: 'random-order',
                ALPHABETICAL_ORDER: 'alphabetical_order',
                REVERSE_ALPHABETICAL_ORDER: 'reverse_alphabetical_order'
            },
    
            REVIEW_CARDS_ORDER: {
                LARGEST_INTERVAL: 'largest-interval',
                SMALLEST_INTERVAL: 'smallest-interval',
                RANDOM: 'RANDOM'
            }
        },
    
        extend: 'Ext.data.Model',
    
        fields: [
            { name: 'id',            type: Ext.data.Types.INTEGER, fieldOption: 'PRIMARY KEY' },
            { name: 'name',          type: Ext.data.Types.STRING},
            { name: 'user_id',       type: Ext.data.Types.INTEGER},
            { name: 'new_cards_order', type: Ext.data.Types.STRING, defaultValue: Pro.LP.Deck.NEW_CARDS_ORDER.IN_ORDER}
        ]
    });
    This is how I got it to work, but i don't like having to repeat the code twice. If I don't define it up the top I can't use it in for example the field definitions Is there a better way of doing this?

  4. #4
    Sencha User Zyclops's Avatar
    Join Date
    Jul 2007
    Location
    Adelaide, South Australia
    Posts
    154
    Vote Rating
    0
    Zyclops is on a distinguished road

      0  

    Default


    Ok the code before ran into problems.. i've now just created an external namespace with the same name as the model. It's the only thing I could get to work consistently.

    Code:
    Ext.ns('LP.Study.statics.Card');
    LP.Study.statics.Card = {
        STATUSES: {
            NEW: 'new',
            REVIEW: 'review',
            FAILED: 'failed'
        },
        ANSWERS: {
            AGAIN: 'again',
            OK: 'ok',
            EASY: 'easy',
            VERYEASY: 'very-easy'
        }
    };
    
     /**
     *
     * The card is what the user sees
     * @extends Object
     */
    Ext.define('LP.Study.model.Card', {
        requires: ['Ext.DateExtras', 'LP.util'],
        extend: 'Ext.data.Model',
        fields: [
            { name: 'id',                   type: Ext.data.Types.INTEGER, fieldOption: 'PRIMARY KEY'},
            { name: 'status',               type: Ext.data.Types.STRING, defaultValue: LP.Study.statics.Card.STATUSES.NEW},
       ]

  5. #5
    Sencha User castitas's Avatar
    Join Date
    Sep 2011
    Location
    US
    Posts
    116
    Answers
    3
    Vote Rating
    3
    castitas is on a distinguished road

      0  

    Default


    I like your solution.

    I did some poking around, and I came up with this.

    Code:
    Ext.define('person',{
        extend: Ext.Base,
        statics: {john:"the man"}
    })
    
    //Statics on an instance
    bob = new person(); //normally, use Ext.create()
    console.log("On instance: ",bob.statics().john)   //"the man"
    
    //Statics on the class
    console.log("On class: ",person.prototype.statics().john)    //"the man"
    As you can see, in order to access the statics without an instance of the class, you need to go through the prototype.
    word

  6. #6
    Sencha - Community Support Team friend's Avatar
    Join Date
    Apr 2011
    Posts
    895
    Answers
    106
    Vote Rating
    27
    friend has a spectacular aura about friend has a spectacular aura about

      0  

    Default


    You may have already done this, but take a look at statics() in Ext.Base. The docs show proper usage and an example of extending a class with statics.

Thread Participants: 3

Tags for this Thread