Results 1 to 6 of 6

Thread: Custom Store Constructor? Better Call Parent First

  1. #1
    Sencha Premium Member
    Join Date
    Aug 2013
    Location
    Chicago, IL
    Posts
    24
    Answers
    2

    Default Answered: Custom Store Constructor? Better Call Parent First

    Sencha,

    If you extend the Ext.data.Store class in 5.0.0 and want to initialize its filters collection in the constructor, why do you now need to first invoke the callParent method before you can safely do so?

    Below is a fiddle demonstrating this phenomenon. Run it as is using the 4.2.1 framework, and the store is initialized. Run as is using the 5.0.0.970, and an error is thrown. Move the me.callParent() statement to the beginning of the stores constructor and the error disappears. This work around works, but why?

    @brady


  2. Quote Originally Posted by LesJ View Post
    I'd think this inconsistency with 4.2.2 might be considered a bug or at least this should be documented in the upgrade guide.
    It would be a bug - a bug in 4.2.

    ExtJS introduced the config object in version 4. The rules for using config were:
    • don't set anything _except_ the config values before the constructor;
    • never set anything in the config _after_ the constructor.
    Ext JS 5 has expanded on the power of configuration, but hasn't changed the rules.

  3. #2
    Sencha Premium User
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    2,372
    Answers
    20

    Default

    I'd think this inconsistency with 4.2.2 might be considered a bug or at least this should be documented in the upgrade guide.

  4. #3
    Sencha Premium Member twasink's Avatar
    Join Date
    Oct 2012
    Location
    Brisbane, Australia
    Posts
    124
    Answers
    4

    Default Because you're setting a value that's expected to be configured

    The filters value is a configuration value; it's expected to be passed in via the config object or set using the generated setter method.

    When the configuration tries to run during the parent constructor, the existing value interferes, and things go wrong.

    Two options:
    • do the initialisation first.
    • pass your custom filters in via the configuration, e.g.:
    Code:
    constructor:  function(config) {  var me = this;
      config = config || {}
      config.filters = me.getMyFilters();
      me.callParent([config]);
    },

  5. #4
    Sencha Premium User
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    2,372
    Answers
    20

    Default

    Good pointer! This also works:

    Code:
            constructor:  function(config) {
                var me = this;
                me.callParent([config])
                me.setConfig("filters", me.getMyFilters());
        },

  6. #5
    Sencha Premium Member twasink's Avatar
    Join Date
    Oct 2012
    Location
    Brisbane, Australia
    Posts
    124
    Answers
    4

    Default

    Yeah, that works. Reads kind of weird, though. The setConfig call is equivalent to:

    Code:
    this.setFilters(this.getFilters());
    Here's a good rule of thumb:
    • prior to invoking the parent constructor, you should only change the config object provided.
    • after invoking the parent constructor, you should completely ignore the config object, and access the properties directly (preferably through the generated setters and getters - there are a few classes where these do more than simply wrap the property)
    In many cases, the config object is available as a field on the class - but a simple change puts a prefix on the front, breaking your code.

    See the docs on configuration (and $configPrefixed) for more.

  7. #6
    Sencha Premium Member twasink's Avatar
    Join Date
    Oct 2012
    Location
    Brisbane, Australia
    Posts
    124
    Answers
    4

    Default

    Quote Originally Posted by LesJ View Post
    I'd think this inconsistency with 4.2.2 might be considered a bug or at least this should be documented in the upgrade guide.
    It would be a bug - a bug in 4.2.

    ExtJS introduced the config object in version 4. The rules for using config were:
    • don't set anything _except_ the config values before the constructor;
    • never set anything in the config _after_ the constructor.
    Ext JS 5 has expanded on the power of configuration, but hasn't changed the rules.

Tags for this Thread

Posting Permissions

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