2012-12-13 165 views
6

如何在类中创建静态字段,然后在Sencha Touch 2中从该类之外访问它们?在Sencha Touch中访问静态属性

例如,我创建了一个简单单用一个静态:

Ext.define('App.util.Config', { 
    singleton: true, 
    statics: { 
     url: { 
      USER: 'http://localhost:3436/api/user' 
     } 
    }, 
    config: { }, 
    constructor: function (config) { 
     this.initConfig(config); 
     this.callParent([config]); 
    } 
}); 

使用我不能访问用户字段App.util.Config.url.USERApp.util.Config .self.url.USER。看着在煎茶文档样本,看来我应该能够能够访问前一种方法的领域:

See Statics Section in this link and how they access the Computer.InstanceCount field

+0

对我工作的罚款。是否App.util.Config.url未定义? App.util.Config.self返回什么? –

+0

App.util.Config.url \t'undefined' App.util.Config.self \t'函数(){ \t返回this.constructor.apply(此,自变量); \t}' App.util.Config.self.url.USER \t'的 “http://本地主机:3436/API /用户”' – Nate

+1

另外一个资料片,可能是相关的,我不是'分机。创建(...)这个类,但是在app.js中需要它:[ 'Ext.MessageBox', 'App.data.ConnectionRouter', 'App.util.Config' ] – Nate

回答

6

我觉得这是你想要

Ext.define('App.util.Config', { 
    singleton: true, 
    statics: { 
     url: { 
      USER: 'http://localhost:3436/api/user' 
     } 
    }, 
    config: { }, 
    constructor: function (config) { 
     var user=this.self.url.User; 
    } 
}); 
1

我什么意识到这是一个古老的问题,但我在寻找别的东西时偶然发现了它。

我相信问题是使用singleton:true。当使用它时,那么所有东西都是静态的,并且不需要将该属性显式定义为静态。

下面列出的是正确的使用方法:

Ext.define('App.util.Config', { 
    singleton: true, 
    url: { 
     USER: 'http://localhost:3436/api/user' 
    }, 
    config: { }, 
    constructor: function (config) { 
     this.initConfig(config); 
     this.callParent([config]); 
    } 
}); 
相关问题