2

다른 초보자 용 질문을하겠습니다. 하위 클래스가 상위 클래스 수준에서 정의 된 함수 및 데이터를 참조하는 여러 가지 방법이 있지만 권장되는 방법이 무엇인지 잘 모르겠습니다. 아래는 제가 다루고있는 예입니다. 그러나이 주제는 부모님과 아이들의 참조와 범위를 잘 이해하지 못하기 때문에 일반적으로 중요합니다. 하위 요소의 하위에서 부모의 함수 및 데이터를 참조하려면 어떻게해야합니까?Sencha Touch 2 - 부모 요소에서 하위 요소를 참조하는 올바른 방법?

언제나처럼, 모든 도움을 주실 수 있습니다.

모하마드

산호세, 캘리포니아

/****** 
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...'); 
    } 
}); 

답변

2

: 여기

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

이 작업 바이올린입니다 그것을 "옳은"방법에 대해 확실하지 않지만, 이것이 내가 원하는 것입니다. 그리고 대부분의 시간은 Sencha 포럼과 자체 코드에서 볼 수 있습니다.

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...'); 
    } 
}); 
+0

예, 이것이 부모 참조를 전달하는 편리한 방법이라고 생각합니다. 감사. – Mohammad