-
20 Sep 2012 5:35 AM #1
Ext.create returns an existing instance
Ext.create returns an existing instance
The call to Ext.create('Pegfect.model.QuestionSet'); returns the same instance previously created with the questions array containing questions previously added to previous instances.Code:Ext.define('Pegfect.model.QuestionSet', { questions: [], skippedFromQuestionSetIndex: undefined, push: function (question) { this.questions.push(question); }, getCount: function() { return this.questions.length; }, first: function () { return this.questions[0]; }, getAt: function(index) { return this.questions[index]; }, getSubDimension: function () { return this.first().subDimension; }, getSubDimensionText: function () { return this.getSubDimension().get('Text'); }, getSubDimensionId: function () { return this.getSubDimension().getId(); } }); var currentQuestionSet; this.rootDimension.subDimensions().each(function (subDimension) { //subDimension.questions().filterBy(function (question) { if (question.hidden == false) return question; }); subDimension.questions().each(function (question) { if (question.hidden) return true; if (!question.get('GroupWithPrevious')) { currentQuestionSet = Ext.create('Pegfect.model.QuestionSet'); me.questionSets.push(currentQuestionSet); qSetCount++; } currentQuestionSet.push(question); qCount++; question.subDimension = subDimension; }); });
Why?
-
20 Sep 2012 5:45 AM #2
It isn't the same instance. Javascript uses prototypes, so if you are just adding to an array or object the property will dereference to the same location as the other instances. You need to actually add the empty array in initComponent or another method.
--edit
whoops not a component but a model. You wouldn't use initComponent, but some other method.
-
20 Sep 2012 5:55 AM #3
Thanks. Its the answer, but I don't understand it. Me being mainly from a C# background, what you are describing to me is a Class member (as opposed to an instance member).
Here's what works, but I'm not following now how the constructor below isnt therefore overwriting the array for ALL classes...
Code:Ext.define('Pegfect.model.QuestionSet', { constructor: function(config) { config = config || {}; this.questions = new Array() }, questions: undefined, skippedFromQuestionSetIndex: undefined, push: function (question) { this.questions.push(question); }, getCount: function() { return this.questions.length; }, first: function () { return this.questions[0]; }, getAt: function(index) { return this.questions[index]; }, getSubDimension: function () { return this.first().subDimension; }, getSubDimensionText: function () { return this.getSubDimension().get('Text'); }, getSubDimensionId: function () { return this.getSubDimension().getId(); } });
-
20 Sep 2012 6:30 AM #4
Prototypical inheritance is not the same as classical inheritance. This isn't immediately obvious with Ext because Ext abstracts some of the ugliness involved with it. In a classical system a class is a blueprint for an object. In a prototype system you don't really have that. A prototype is literally an object. When you call a property on an object that inherits from it, it first checks the object you called. If it is not there it goes up the prototype chain and sees if it is on its parent. When you are setting the property in the constructor it is giving your instance that property. Therefore, it will be unique for each instance you create.
-
24 Sep 2012 11:54 AM #5Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- Frederick MD, NYC, DC
- Posts
- 16,170
- Vote Rating
- 33
Welcome to the world of JavaScript.
I highly suggest you consider reformatting your code so it's easier to be read. To be honest, I was not going to respond because of how the code is formatted, but I couldn't resist.
Ideally, your code should look like (four spaces per indentation, not two!):
Code:Ext.define('Pegfect.model.QuestionSet', { questions: undefined, skippedFromQuestionSetIndex: undefined, constructor: function (config) { config = config || {}; this.questions = []; }, push: function (question) { this.questions.push(question); }, getCount: function () { return this.questions.length; }, first: function () { return this.questions[0]; }, getAt: function (index) { return this.questions[index]; }, getSubDimension: function () { return this.first().subDimension; }, getSubDimensionText: function () { return this.getSubDimension().get('Text'); }, getSubDimensionId: function () { return this.getSubDimension().getId(); } });
Jay Garcia @ModusJesus || Modus Create co-founder
Ext JS in Action author
Sencha Touch in Action author
Get in touch for Ext JS & Sencha Touch Touch Training
We are also working on Video-based Sencha Touch training: Check it out here.
-
24 Sep 2012 11:58 AM #6
I reverted to 2 space indents years ago, i much prefer it the the stickle-brick style 4 indents, its so 1990s.
-
24 Sep 2012 12:26 PM #7
Now if you both switched to using tabs you could maintain the same code and set your editors tab-width to the desired setting and there would be much rejoicing.
-
24 Sep 2012 2:24 PM #8
I use tabs for exactly this reason - and my ide (VS2010) is set to have 2-spaced tabs - just my pref. and no one else's business!
-
24 Sep 2012 5:32 PM #9
If you want people to help you, I would recommend using 4 spaces in the forums (as well as newlines in your functions). Most people are accustomed to larger indentation. I know I will bypass questions if I don't find the code readable enough, so I am sure there are plenty of others that do as well.


Reply With Quote