View Full Version : How do you define static class variables so they can be used in the class?
Zyclops
15 Dec 2011, 1:38 AM
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.
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'
}
});
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?
tvanzoelen
15 Dec 2011, 2:17 AM
Should you reference it with?
MyApp.Deck.NEW_CARDS_ORDER.IN_ORDER
I don't see Deck in your code
Zyclops
27 Dec 2011, 5:24 PM
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?
Zyclops
5 Jan 2012, 11:13 PM
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.
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},
]
castitas
7 Aug 2012, 8:15 AM
I like your solution.
I did some poking around, and I came up with this.
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.
friend
7 Aug 2012, 9:26 AM
You may have already done this, but take a look at statics() (http://docs.sencha.com/ext-js/4-1/#%21/api/Ext.Base-method-statics) in Ext.Base. The docs show proper usage and an example of extending a class with statics.
Powered by vBulletin® Version 4.1.5 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.