【问题标题】:ExtJS 4, store loading, but store.first() method returning undefinedExtJS 4,存储加载,但 store.first() 方法返回未定义
【发布时间】:2012-03-18 13:55:02
【问题描述】:

我正在创建和加载一个商店。但是,当我尝试使用 first() 方法从第一个模型实例中检索记录时,它失败了。

代码如下:

     var getCaseId = function(){
            var csId;    
            //form url
            var getCaseIdStore = Ext.create('Roa.store.district.Requests', {

            model: 'Roa.model.district.Requests',
            proxy: {
                    type: 'ajax',
                    url : 'http://localhost:8080/roa/MongoDataService/requests?etc',
                    method: 'GET',

                    reader: {
                            type: 'json',
                            root: 'requests'
                    }   
            },  
            autoLoad: true
    });//end create store

                    console.log('request store created');

    getCaseIdStore.load({
    scope: this,
    callback: function(records,operation,success){

            if(success){

                    //var req = getCaseIdStore.first();
                    //var csId = req.get('caseId');
                    //console.log('csId==>'+csId);

                   //return csId;  

            }//end if

    }//end callback

    });//end store load


    var req = getCaseIdStore.first();

    if (req != undefined){
    csId = req.get('caseId');

    console.log('return csId==>'+csId);

     return csId;    
     }               
    else{           
    console.log('your attempt failed');
    }               

    };//end function

请好心人指教。

-TU

【问题讨论】:

    标签: load extjs4 store


    【解决方案1】:

    加载过程是异步的。您不能只调用它然后尝试获取第一条记录 - 将您的 .first() 调用移动到存储加载回调中,因为您在注释掉的行中有它。这些行应该可以正常工作。

    【讨论】:

    • .first() 里面的回调很好。
    • 是的,它有效。但是,我在理解如何从回调中获取 csId 值以由包含函数--getCaseId() 返回时遇到了一些困难。
    • 您不能...您正在尝试将包含函数视为同步函数,但是您将在启动加载操作后立即从该函数中获得控制权。您需要重新设计一个调用此函数的位代码,使其不立即依赖它的结果。
    【解决方案2】:

    黑暗中的巨大镜头,但如果您的加载范围是“this”,它可能没有 getCaseIdStore?也许只是将回调中的 getCaseIdStore 替换为“this”?

    【讨论】:

    • 我回答假设注释行是失败的行(在负载内)。如果未注释的行是失败的行,请查看 sha 的回复。
    • 不,注释代码有效。我只是在努力了解如何在 store.load() 方法之外访问返回值——csId。
    【解决方案3】:

    您可以使用 getCaseIdStore.isLoading( ) 方法检查存储何时完成加载操作。然后你可以继续你的 .first() 调用。

    【讨论】:

      猜你喜欢
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 2014-09-16
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多