【问题标题】:Accessing static properties in Sencha Touch在 Sencha Touch 中访问静态属性
【发布时间】:2012-12-13 13:49:33
【问题描述】:

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

例如,我创建了一个带有单个静态的简单单例:

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.USER 但使用 App.util.Config.self.url.USER 访问 USER 字段。查看 Sencha 文档上的示例,看来我应该能够以前一种方式访问​​该字段:

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

【问题讨论】:

  • 对我来说工作得很好。 App.util.Config.url 是未定义的吗? App.util.Config.self 返回什么?
  • App.util.Config.url undefined App.util.Config.self function () { return this.constructor.apply(this, arguments); } App.util.Config.self.url.USER "http://localhost:3436/api/user"
  • 另一条可能相关的信息,我不是Ext.create(...)这个类,但在app.js中需要它requires: [ 'Ext.MessageBox', 'App.data.ConnectionRouter', 'App.util.Config' ],

标签: extjs sencha-touch-2


【解决方案1】:

我想这就是你想要的

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;
    }
});

【讨论】:

    【解决方案2】:

    我意识到这是一个老问题,但我在寻找其他东西时偶然发现了它。

    我认为问题在于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]);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多