1. #1
    Sencha User
    Join Date
    Jun 2012
    Posts
    82
    Vote Rating
    1
    dr-dan is on a distinguished road

      0  

    Default Ext.create returns an existing instance

    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
    536
    Vote Rating
    70
    Tim Toady is a jewel in the rough Tim Toady is a jewel in the rough Tim Toady is a jewel in the rough Tim Toady is a jewel in the rough

      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
    82
    Vote Rating
    1
    dr-dan is on a distinguished road

      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
    536
    Vote Rating
    70
    Tim Toady is a jewel in the rough Tim Toady is a jewel in the rough Tim Toady is a jewel in the rough Tim Toady is a jewel in the rough

      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
    jay@moduscreate.com's Avatar
    Join Date
    Mar 2007
    Location
    Frederick MD, NYC, DC
    Posts
    16,353
    Vote Rating
    77
    jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all

      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
    82
    Vote Rating
    1
    dr-dan is on a distinguished road

      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
    536
    Vote Rating
    70
    Tim Toady is a jewel in the rough Tim Toady is a jewel in the rough Tim Toady is a jewel in the rough Tim Toady is a jewel in the rough

      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.

  8. #8
    Sencha User
    Join Date
    Jun 2012
    Posts
    82
    Vote Rating
    1
    dr-dan is on a distinguished road

      0  

    Default


    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!

  9. #9
    Sencha User Tim Toady's Avatar
    Join Date
    Feb 2010
    Location
    Pennsylvania
    Posts
    536
    Vote Rating
    70
    Tim Toady is a jewel in the rough Tim Toady is a jewel in the rough Tim Toady is a jewel in the rough Tim Toady is a jewel in the rough

      0  

    Default


    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.