1. #1
    Sencha User
    Join Date
    Oct 2012
    Posts
    1
    Vote Rating
    0
    tmarly is on a distinguished road

      0  

    Default Unanswered: How to keep a reference to the parent container from a view item ?

    Unanswered: How to keep a reference to the parent container from a view item ?


    Hi,

    I defined a view, with a class attribute 'comment', and I want to open a dialog (prompt) containing this attribute, and save the entered value in the attribute.

    Here is the code:

    Code:
    Ext.define('org.MyComponent', {
      extend: 'Ext.Panel',
      xtype: 'mycomponent',
    
      config: {
    
        comment: '',
    
         items: [
           {
              xtype: 'button',
               text: 'edit',
               handler: function() {
                 Ext.Msg.prompt(
                   'Comment', 
                   null, 
                   function(btn, text){
                     if(btn == 'ok'){
                       // #comment1 - do not work
                       this.parent.config.comment = text;
                     }
                   },
                   null, 
                   true, 
                   // #comment2 - 'this.parent' not very nice
                   this.parent.config.comment, 
                   null
                 );
               } 
             },
           ]
         },
    
    });
    (then, when I create this component, I'm giving 'comment' in the constructor).

    As you can see, there are 2 "this.parent.config.comment". One of them is not working ('#comment1'), and I think the other ('#comment2') is not really elegant.

    My main concern being that 'this' do not refer to MyComponent instance.

    So I'm wondering what is the best conception / pattern to achieve this.

    For the occurence '#comment2', would it be better to initalize the value, in the constructor for example, using add([...]) ? Any example ?

    For the occurence '#comment1', would it be better to use listeners ? If yes, how ?

    Any other suggestion ?

    Tristan

  2. #2
    Sencha User
    Join Date
    Apr 2010
    Location
    China
    Posts
    227
    Answers
    64
    Vote Rating
    20
    haduki will become famous soon enough haduki will become famous soon enough

      0  

    Default


    Code:
    Ext.define('org.MyComponent', {
        extend: 'Ext.Panel',
        xtype: 'mycomponent',
        config: {
            comment: '',
            items: [{
                xtype: 'button',
                text: 'edit',
                handler: function() {
                    var mycomponent = this.up('panel');
                    Ext.Msg.prompt('Comment', null,
                    function(btn, text) {
                        if (btn == 'ok') { 
                            mycomponent.setComment(text);
                        }
                    },
                    null, true,
                    mycomponent.getComment(), null);
                }
            },
            ]
        },
    });
    I write English by translator.

Thread Participants: 1

Tags for this Thread