Results 1 to 7 of 7

Thread: Wrong reference when creating objects from extended class

  1. #1

    Default Wrong reference when creating objects from extended class

    Hi,
    I have tried to extend the GroupingStore with an extra function by doing this:

    Code:
    Om.data.OrderDataStore = Ext.extend(Ext.data.GroupingStore, {
        id : 'OrderDataStore',
        setFilterParameter : function (parameter) {
            this.baseParams['filter'] = parameter;
        }
       ...
    Then I try to create two instances of this derived class:

    Code:
    var ordersWithBA = new Om.data.OrderDataStore();
    ordersWithBA.setFilterParameter("ORDER_BA_OPEN");
    
    var ordersWithBADelivered = new Om.data.OrderDataStore();
    ordersWithBADelivered.setFilterParameter("ORDER_BA");
    which results in overwriting the first variables filter parameter (ORDER_BA_OPEN) with the second one (ORDER_BA).

    Why does this happen? Don't I have two different variables?!?

    Thanks for any help,
    Oliver

  2. #2
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    1. You can't specify an id in the prototype if you are planning to create multiple instances (all instances would have the same id, which isn't allowed).
    2. This shouldn't happen. Are you sure you didn't also specify baseParams in the prototype (you didn't include your full extended class).

  3. #3

    Default

    Hi,
    thanks for the quick reply!

    1. I have removed the id from the inheritance code and put it into the constructor as parameter but this doesn't work either:

    Code:
    var ordersWithBA = new Om.data.OrderDataStore({ id : 'OrderDataStoreWithBA'});
    ordersWithBA.setFilterParameter("ORDER_BA_OPEN");
    So, my question would be how do I have to inherit the class to be able to pass and override the config parameters of the superclass?

    EDIT: I added "Ext.apply(this, config); " to the Om.data.Orderstore function declaration but doesn't work...

    2. Yes, I have baseParams initialized which are overwritten by the setFilterParameter() function. This works but on both instances. The full code is now like this (changed the extend call as described here: http://www.sencha.com/learn/Manual:Intro:Inheritance

    Code:
    // DataStore for a complete order
    Om.data.OrderDataStore = function(config) {
        // call parent constructor
        Om.data.OrderDataStore.superclass.constructor.call(this, config);
    }; 
    Ext.extend(Om.data.OrderDataStore, Ext.data.GroupingStore, {
        proxy : new Ext.data.HttpProxy( {
            url : 'data.php', // File to connect to
            method : 'POST'
        }),
        baseParams : {
            action : "LIST_ORDERS"
        }, // this parameter asks for listing
        reader : new Ext.data.JsonReader( {
            root : 'results',
            totalProperty : 'total',
            id : 'id'
        }, [ {
            name : 'id',
            type : 'int',
            mapping : 'id'
        }, {
            name : 'ba_number',
            type : 'int',
            mapping : 'ba_number'
        }, {
            name : 'ordered_at',
            type : 'date',
            dateFormat : 'd.m.Y H:i:s',
            mapping : 'ordered_at'
        }, {
            name : 'ordered_by',
            type : 'string',
            mapping : 'ordered_by'
        }, {
            name : 'product',
            type : 'string',
            mapping : 'product'
        }, {
            name : 'vendor',
            type : 'string',
            mapping : 'vendor'
        }, {
            name : 'order_code',
            type : 'int',
            mapping : 'order_code'
        }, {
            name : 'amount',
            type : 'string',
            mapping : 'amount'
        }, {
            name : 'current_amount',
            type : 'string',
            mapping : 'current_amount'
        }, {
            name : 'price_total',
            type : 'float',
            mapping : 'price_total'
        }, {
            name : 'cost_number',
            type : 'int',
            mapping : 'cost_number'
        }, {
            name : 'delivered_at',
            type : 'date',
            dateFormat : 'd.m.Y H:i:s',
            mapping : 'delivered_at'
        }, {
            name : 'accepted_by',
            type : 'string',
            mapping : 'accepted_by'
        }, {
            name : 'storage_location',
            type : 'string',
            mapping : 'storage_location'
        }, {
            name : 'comments',
            type : 'string',
            mapping : 'comments'
        } ]),
        sortInfo : {
            field : 'id',
            direction : "ASC"
        },
        setFilterParameter : function (parameter) {
            this.baseParams['filter'] = parameter;
        }
    });

  4. #4
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    Do NOT specify complex objects (like proxy, baseParams, reader and sortInfo) in the prototype. Remember that they will be shared by all instances.

    Instead, create these objects in the constructor (or initComponent if it is an Ext.Component).

  5. #5

    Default

    Hi,
    I understand but why can't I overwrite the id in the constructor?

    And generally:
    Is this a general JS "problem" or only extJS specific?

    Because normally I would expect two "baseParam" objects when creating two instances. I understood the prototyping like a class declaration e.g. in Java.

    EDIT: What sense does it make then to inherit in ExtJs when I need to put the whole configuration in every instance declaration?!? Seems weird to me

    Thanks for your help anyway!

    Oliver

  6. #6
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    I think you're missing the point.

    I meant you should be using:
    Code:
    Om.data.OrderDataStore = Ext.extend(Ext.data.GroupingStore, {
        constructor: function (config) {
            Om.data.OrderDataStore.superclass.constructor.call(this, Ext.apply({
                proxy: new Ext.data.HttpProxy({
                    url: 'data.php',
                    method: 'POST'
                }),
                baseParams: {
                    action: "LIST_ORDERS"
                },
                reader: new Ext.data.JsonReader({
                    root: 'results',
                    totalProperty: 'total',
                    idProperty: 'id',
                    fields: [{
                        name: 'id',
                        type: 'int',
                        mapping: 'id'
                    }, {
                        name: 'ba_number',
                        type: 'int',
                        mapping: 'ba_number'
                    }, {
                        name: 'ordered_at',
                        type: 'date',
                        dateFormat: 'd.m.Y H:i:s',
                        mapping: 'ordered_at'
                    }, {
                        name: 'ordered_by',
                        type: 'string',
                        mapping: 'ordered_by'
                    }, {
                        name: 'product',
                        type: 'string',
                        mapping: 'product'
                    }, {
                        name: 'vendor',
                        type: 'string',
                        mapping: 'vendor'
                    }, {
                        name: 'order_code',
                        type: 'int',
                        mapping: 'order_code'
                    }, {
                        name: 'amount',
                        type: 'string',
                        mapping: 'amount'
                    }, {
                        name: 'current_amount',
                        type: 'string',
                        mapping: 'current_amount'
                    }, {
                        name: 'price_total',
                        type: 'float',
                        mapping: 'price_total'
                    }, {
                        name: 'cost_number',
                        type: 'int',
                        mapping: 'cost_number'
                    }, {
                        name: 'delivered_at',
                        type: 'date',
                        dateFormat: 'd.m.Y H:i:s',
                        mapping: 'delivered_at'
                    }, {
                        name: 'accepted_by',
                        type: 'string',
                        mapping: 'accepted_by'
                    }, {
                        name: 'storage_location',
                        type: 'string',
                        mapping: 'storage_location'
                    }, {
                        name: 'comments',
                        type: 'string',
                        mapping: 'comments'
                    }]
                }),
                sortInfo: {
                    field: 'id',
                    direction: "ASC"
                }
            }, config));
        },
        setFilterParameter: function (parameter) {
            this.baseParams['filter'] = parameter;
        }
    });
    Now it creates a separate instance of proxy, baseParams, reader and sortInfo for every store instance.

  7. #7

    Default

    Got it! Thank you very much for your help!

Similar Threads

  1. How to reference generated objects properly
    By madirishman in forum Ext Designer: Help & Discussion
    Replies: 3
    Last Post: 30 Aug 2010, 7:19 AM
  2. howto get this keyword to reference extended form within listener
    By rich71 in forum Ext 3.x: Help & Discussion
    Replies: 2
    Last Post: 27 Jul 2010, 5:48 AM
  3. [INFOREQ] Documentation Viewer for Extended Objects.
    By CrazyEnigma in forum Ext 3.x: Help & Discussion
    Replies: 2
    Last Post: 16 Jun 2010, 5:54 AM
  4. Creating extended class from large chunk of code
    By mmartinek in forum Ext 3.x: Help & Discussion
    Replies: 0
    Last Post: 29 Oct 2009, 7:52 PM
  5. Dynamically creating UI objects
    By ender07 in forum Ext 2.x: Help & Discussion
    Replies: 1
    Last Post: 3 Jan 2009, 2:32 AM

Posting Permissions

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