1. #1
    Sencha User
    Join Date
    Mar 2012
    Posts
    5
    Vote Rating
    0
    Cualit is on a distinguished road

      0  

    Default What are the Downsides in Passing a Model Record in Constructor config?

    What are the Downsides in Passing a Model Record in Constructor config?


    Suppose the following scenario:

    var myRecord = store.getAt(0);var myClass = Ext.Create('MyApp.MyClass', {record: myRecord});
    • What are the downsides in passing a Model Record (that is already in a store) to a Constructor ?
    • If I access the record inside MyClass constructor like this:
      constructor: function (config) {

      config.record the same object reference as myRecord?
    • What happens to myRecord once myClass get's destroyed ?
    • Is this a good practice ? I'm used to Java, and to pass object references as parameters in constructors
    • Is this a better approach? why?:
      var myRecord = store.getAt(0);
      var myClass = Ext.Create('MyApp.MyClass', {});
      myClass.myInit({record: myRecord});
    Thank you! I'll appreciate your answers!

  2. #2
    Sencha - Support Team slemmon's Avatar
    Join Date
    Mar 2009
    Location
    Boise, ID
    Posts
    5,011
    Vote Rating
    183
    slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold

      0  

    Default


    You should be just fine using:
    Code:
    var myRecord = store.getAt(0);
    var myClass = Ext.Create('MyApp.MyClass', {
        record: myRecord
    });
    You can see in the below example that I created a store and used that store in a grid. I grabbed a reference to one of the records and passed it to my window component. By default closing a window will destroy it and you can see that the record is not destroyed with the window is closed - if it were it'd remove the row form the grid relating to the record.

    If you wanted to destroy the record you'd have to actually do so explicitly - see destroy listener in the example below.

    Code:
    var store = Ext.create('Ext.data.Store', {
        storeId:'simpsonsStore',
        fields:['name', 'email', 'phone'],
        data:{'items':[
            { 'name': 'Lisa',  "email":"lisa@simpsons.com",  "phone":"555-111-1224"  },
            { 'name': 'Bart',  "email":"bart@simpsons.com",  "phone":"555-222-1234" },
            { 'name': 'Homer', "email":"home@simpsons.com",  "phone":"555-222-1244"  },
            { 'name': 'Marge', "email":"marge@simpsons.com", "phone":"555-222-1254"  }
        ]},
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'items'
            }
        }
    });
    
    
    
    
    Ext.create('Ext.grid.Panel', {
        title: 'Simpsons',
        store: Ext.data.StoreManager.lookup('simpsonsStore'),
        columns: [
            { text: 'Name',  dataIndex: 'name' },
            { text: 'Email', dataIndex: 'email', flex: 1 },
            { text: 'Phone', dataIndex: 'phone' }
        ],
        height: 200,
        width: 400,
        renderTo: Ext.getBody()
    });
    
    
    
    
    var myRecord = store.getAt(0);
    
    
    Ext.widget('window', {
        title: 'Close me and see what happens to my record',
        autoShow: true,
        height: 100,
        width: 400,
        html: 'On closing the window it is destroyed, but the record is not<br>If it were you\'d see it pop out of the store.',
        record: myRecord,
        listeners: {
            show: function () {
                console.log(this.record);
            },
            destroy: function () {
                // this.record.destroy();
                // the above line when uncommented will destroy the record, but will also generate an error in 4.2.1 - bug has been reported: EXTJSIV-10309
            }
        }
    });
    Are you a Sencha products veteran who has wondered what it might be like to work at Sencha? If so, please reach out to our recruiting manager: sheryl@sencha.com

    C
    heck out all of the 2013 SenchaCon presentations here:
    http://www.sencha.com/blog/senchacon...now-available/

  3. #3
    Sencha User
    Join Date
    Mar 2012
    Posts
    5
    Vote Rating
    0
    Cualit is on a distinguished road

      0  

    Default


    Awesome, thank you very much!

Thread Participants: 1

Tags for this Thread