Results 1 to 9 of 9

Thread: Ext.create returns an existing instance

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Sencha User
    Join Date
    Jun 2012
    Posts
    83
    Vote Rating
    3
      0  

    Default Ext.create returns an existing instance

    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;
          });
        });
    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.

    Why?

  2. #2
    Sencha User Tim Toady's Avatar
    Join Date
    Feb 2010
    Location
    Pennsylvania
    Posts
    603
    Vote Rating
    284
      0  

    Default

    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.

  3. #3
    Sencha User
    Join Date
    Jun 2012
    Posts
    83
    Vote Rating
    3
      0  

    Default

    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(); }
    });

  4. #4
    Sencha User Tim Toady's Avatar
    Join Date
    Feb 2010
    Location
    Pennsylvania
    Posts
    603
    Vote Rating
    284
      0  

    Default

    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.

  5. #5
    Sencha User jay@moduscreate.com's Avatar
    Join Date
    Mar 2007
    Location
    DC Area =)
    Posts
    16,364
    Vote Rating
    87
      0  

    Default

    Quote Originally Posted by dr-dan View Post
    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).

    [/CODE]
    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();
        }
    });

  6. #6
    Sencha User
    Join Date
    Jun 2012
    Posts
    83
    Vote Rating
    3
      0  

    Default

    I reverted to 2 space indents years ago, i much prefer it the the stickle-brick style 4 indents, its so 1990s.

  7. #7
    Sencha User Tim Toady's Avatar
    Join Date
    Feb 2010
    Location
    Pennsylvania
    Posts
    603
    Vote Rating
    284
      0  

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •