-
15 Dec 2011 1:38 AM #1
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' } });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.Code:Ext.define('MyApp.Deck', { extend: 'Ext.data.Model', fields: [ { name: 'new_cards_order', type: 'string', defaultValue: MyApp.NEW_CARDS_ORDER.IN_ORDER } ] })
Any ideas?
-
15 Dec 2011 2:17 AM #2Ext JS Premium Member
- Join Date
- Apr 2008
- Location
- Groningen - Netherlands
- Posts
- 1,017
- Vote Rating
- 23
- Answers
- 75
Should you reference it with?
I don't see Deck in your codeCode:MyApp.Deck.NEW_CARDS_ORDER.IN_ORDER
-
27 Dec 2011 5:24 PM #3
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?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} ] });
-
5 Jan 2012 11:13 PM #4
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}, ]
-
7 Aug 2012 8:15 AM #5
I like your solution.
I did some poking around, and I came up with this.
As you can see, in order to access the statics without an instance of the class, you need to go through the prototype.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"word
-
7 Aug 2012 9:26 AM #6
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.



Reply With Quote