【问题标题】:Making an Ajax call inside a singleton class constructor在单例类构造函数中进行 Ajax 调用
【发布时间】:2014-01-15 03:30:57
【问题描述】:

使用 Sencha Touch,我在其构造函数中定义了一个带有 Ajax 调用的单例类。当我的应用程序启动时,我收到错误消息:Cannot call method 'request' of undefined 从带有“Ext.Ajax.request”的行中调用。

如果我在构造函数中删除 Ajax 调用,我的应用程序将加载而不会出现任何错误。

然后,如果我在我的应用程序启动后调用的另一个函数中进行 Ajax 调用,它就可以工作。所以我假设调用构造函数时 Ext.Ajax 尚未加载。我尝试将 uses 参数添加到我的类,但它不起作用。

Ext.define('MyApp.utils.PostalCode', {
    singleton: true,
    config : {
        postalCodesHashMap : null,  
    },  
    uses : 'Ext.Ajax',
    constructor: function(config) {
        this.initConfig(config);
        Ext.Ajax.request({
            //will not work        
        }); 
    },  
    loadData : function() {
        Ext.Ajax.request({
            //will work             
        }); 
    }
});

【问题讨论】:

  • @jprofitt :它有效,谢谢!你可以做一个答案,我会选择它作为正确答案。

标签: ajax extjs constructor singleton sencha-touch-2


【解决方案1】:

您应该使用requires 来指示在创建此类之前需要Ext.Ajax,而不是使用uses

requires: ['Ext.Ajax']

可能发生的情况是,在此过程中需要Ext.Ajax,因此loadData() 请求可以访问它,而无需MyApp.utils.PostalCode 明确要求首先加载它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 2019-03-14
    • 1970-01-01
    • 2012-07-11
    • 2018-03-31
    • 1970-01-01
    相关资源
    最近更新 更多