2011-03-07 51 views
1

类中的某个方法是否可以存储其他方法稍后可以使用的值?Sencha Touch/ExtJS:如何存储变量以备后用?

例如,这里是从我的Ext.Panel实例中的一个片段:

app.views.EstablishmentDetail = Ext.extend(Ext.Panel, { 
dockedItems: [{ 
    xtype: 'toolbar', 
    title: 'View establishment', 
    items: [ 
     { 
      text: 'Back', 
      ui: 'back', 
      listeners: { 
       'tap': function() { 
        Ext.dispatch({ 
         controller: app.controllers.establishments, 
         action: 'index', 
         animation: {type:'slide', direction:'right'} 
        }); 
       } 
      } 
     }, 
     {xtype:'spacer'}, 
     { 
      id: 'Map', 
      text: 'Map', 
      ui: 'action', 
      listeners: { 
       'tap': function() { 
        Ext.dispatch({ 
         controller: app.controllers.establishments, 
         action: 'showMap', 
         id: this.record.getId(), 
         data: this.record.data, 
         record: this.record 
        }); 
       } 
      } 
     } 
    ] 
}], 
styleHtmlContent:true, 
scroll: 'vertical', 

我想有一个变量在这个分类 - 称之为“MYVARIABLE”我可以把信息传给您在上面看到的调度程序中的属性'showMap'。 (我想从另一个方法调用初始化它的值。)

但是在ExtJS上看到的所有例子中,当你将类Ext.Panel这样子类化时,看起来你只能引用预先存在的属性(如'标题')或创建新的功能。在一次方法调用期间是否存在一种保存变量的机制,然后在另一种方法中使用该变量?

回答

4

是的,你可以添加你想要的任何属性到一个子类。关于像你这样的方法之间共享数据的唯一棘手的事情是,这个指针指向的是项目,而不是子类。

我觉得容易的事是重写initComponent方法,这样就可以使用闭包你处理程序之间共享变量,例如:

app.views.EstablishmentDetail = Ext.extend(Ext.Panel, { 
initComponent: function() { 
    var me = this; //me will always refer to the view instance 
    me.myVariable = yourMethod(); 
    me.dockedItems = [ 
     ...same code as you had before { 
     listeners: { 
      'tap': function() { 
       Ext.dispatch({ 
        someProp: me.myVariable 
       }); 
      } 
     } 
    }]; 

    //You have to remember to do this anytime you override the initComponent method!!! 
    app.views.EstablishmentDetail.superclass.initComponent.apply(this, arguments); 
}}); 

恕我直言,任何时候你正在使用该指针在子类,如果将代码移动到initComponent并使用闭包,它将不那么令人困惑。你不需要,但是你必须上下移动父/子组件链。

希望这有助于

+0

谢谢杰森。我一直在尝试您的建议,并已将'我'添加到属性列表中,如下所示:'me:{}'。 然后,我按照上面列出的方式进行操作,在initComponent中用'me = this'初始化它。我这样做的原因是,如果我将'var me'放入initComponent中,无论出于何种原因,'我'在后面的Ext.dispatch调用中都不存在。 (请注意,我保留我的dockedItems数组原样,并没有将其转换为'me.dockedItems') 我不确定为什么会出现这种情况,但是我所做的工作似乎可行。非常感谢您的帮助。 – larryq 2011-03-07 18:07:35

1

把变量下的类是使用Ext.apply方法的一个很好的方式。

Ext.apply(this, { 
myVar1 : myVar1, 
myProp1: myProp1, 
}) 

Dispatch调用中不存在变量的原因是它不在正确的范围内。在这种情况下,侦听器函数的默认范围是工具栏。您必须在侦听器上将范围设置为此。

要回答你的问题,你可以做这样的事情:

initComponent: function(){ 
    Ext.apply(this, { 
    dispatchAction : 'showMap' 
    }); 
    app.views.EstablishmentDetail.superclass.initComponent.call(this); 
} 

...

listeners: { 
       tap:{ 
        fn : function() { 
        Ext.dispatch({ 
         controller: app.controllers.establishments, 
         action: this.dispatchAction, 
         id: this.record.getId(), 
         data: this.record.data, 
         record: this.record 
        }); 
       } 
       scope: this 
      } 
     } 

有使用监听器的许多有效的方式反对,这是其中之一。我更喜欢使用on/addListener方法。

+0

谢谢你的解释(和代码片段)。 – larryq 2011-03-10 19:48:54