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

Я m не уверен в «правильном» способе сделать это, но это то, что я хотел бы и что я вижу большую часть времени на форуме 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

1

Вы можете использовать prototype для ее достижения:

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

Вот рабочая скрипку: http://www.senchafiddle.com/#MvABI

+0

Спасибо за ввод. Я еще не использовал прототип в ST2, но это хорошее напоминание. – Mohammad

Смежные вопросы