2

I'm going to ask another newbie question. I have come across multiple ways for a child to reference functions and data defined at the parent class level, but I'm not sure what is the recommended way. Below is an example that I am dealing with, but this topic is generally important for me since I do not have a good understanding of reference and scope of parents and children. How can I reference functions and data of a parent from a child of a child element?

As usual, any help will be highly appreciated.

Mohammad

San Jose, CA

/******
This is a floating panel with a formpanel inside it, that has an email field and a button to process the email. 
When the button is tapped, I want to call the processEmails() function from the button.
******/
Ext.define('myapp.view.ForwardPanel', {
    extend  : 'Ext.Panel',
    xtype   : 'forwardPanel',
    initialize: function() {
        this.callParent(arguments);
        var btn = {
            xtype: 'button',
            ui: 'action',
            text: 'Send',
            listeners: {
                tap: function(){
                    processEmails(); //<- **How can I reference and call this function?**
                                 // This function is defined at the parent class level 
                                 // (indicated at the bottom of the code listing)
             }}
        };
        var form = {
            items: [{
                    xtype: 'fieldset',
                    instructions: 'Enter multiple emails separated by commas',
                    title: 'Forward this message.',
                    items: [ {
                            xtype: 'emailfield',
                            name: 'email',
                            label: 'Email(s)'
                    },btn]             // The button is added to the form here
                }]
        };
        this.add(form);
    },

    // this is the parent level function I want to call upon button tap.
    processEmails: function(){
        console.log('Processing email...');
    }
});
André Dion
  • 21,269
  • 7
  • 56
  • 60
rc1
  • 483
  • 4
  • 19

2 Answers2

2

I'm not sure about a "right" way to do it, but this is what I would do, and what I see most of the time in the Sencha forum and in their own code:

Ext.define('myapp.view.ForwardPanel', {
  ...
  initialize: function() {
    this.callParent(arguments);
    var me = this;     // <---- reference to the "ForwardPanel" instance
    var btn = {
        xtype: 'button',
        ui: 'action',
        text: 'Send',
        listeners: {
          tap: function(){
            me.processEmails(); // <-- notice the "me" in front
          }
        }
    };
    ...
  },

  // this is the parent level function I want to call upon button tap.
  processEmails: function(){
    console.log('Processing email...');
  }
});
fractious
  • 1,642
  • 16
  • 30
Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
  • Yeah, I guess this is a convenient way to pass along the parent reference. Thanks. – rc1 Feb 14 '13 at 00:59
1

You can make use of prototype to achieve it:

myapp.view.MyView.prototype.processEmails.call(this);

Here is a working fiddle: http://www.senchafiddle.com/#MvABI

Community
  • 1
  • 1
Eli
  • 14,779
  • 5
  • 59
  • 77
  • Thanks for the input. I have not used prototype in ST2 yet, but it's a good reminder. – rc1 Feb 14 '13 at 00:59