最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Extjs store.getCount() returns 0 - Stack Overflow

programmeradmin0浏览0评论

I am trying to load data to store using a json file. But getCount is returning 0.Store and asset.json are in the same folder. What changes do I need to make?

Code for the Model:

Ext.define('Nm.model.analysis.Asset', {

     extend: 'Ext.data.Model',
     requires: [
                'Ext.data.Field'
            ],
     fields: [
              {
                  name: 'assetType',
                  type: 'string'
              },
              {
                  name: 'referenceNo',
                  type: 'string'
              },
                  {
                  name: 'action',
                  type: 'int'
              }
                ]
   });

Store I am trying to create :

var store = Ext.create('Ext.data.Store', {
     model: 'Nm.model.analysis.Asset',
     proxy: {
        type: 'rest',
        url : 'asset.json',
        reader: {
            type: 'json',
            rootProperty: 'assets'
        }
        },
        listeners : {
        load : function(store) {
            alert('getcount from listener '+store.getCount());
        }
    }
 });

store.load();
 alert("getcount="+store.getCount());

Data in asset.json

{
    "assets" : [
                {
                    "assetType" : 'hello',
                    "referenceNo" : 'refNo.1',
                    "action" : 2
                },
                {
                    "assetType" : 'new',
                    "referenceNo" : 'refNo.2'
                }
    ]
}

I am trying to load data to store using a json file. But getCount is returning 0.Store and asset.json are in the same folder. What changes do I need to make?

Code for the Model:

Ext.define('Nm.model.analysis.Asset', {

     extend: 'Ext.data.Model',
     requires: [
                'Ext.data.Field'
            ],
     fields: [
              {
                  name: 'assetType',
                  type: 'string'
              },
              {
                  name: 'referenceNo',
                  type: 'string'
              },
                  {
                  name: 'action',
                  type: 'int'
              }
                ]
   });

Store I am trying to create :

var store = Ext.create('Ext.data.Store', {
     model: 'Nm.model.analysis.Asset',
     proxy: {
        type: 'rest',
        url : 'asset.json',
        reader: {
            type: 'json',
            rootProperty: 'assets'
        }
        },
        listeners : {
        load : function(store) {
            alert('getcount from listener '+store.getCount());
        }
    }
 });

store.load();
 alert("getcount="+store.getCount());

Data in asset.json

{
    "assets" : [
                {
                    "assetType" : 'hello',
                    "referenceNo" : 'refNo.1',
                    "action" : 2
                },
                {
                    "assetType" : 'new',
                    "referenceNo" : 'refNo.2'
                }
    ]
}
Share Improve this question asked Nov 18, 2014 at 6:52 AnkitaAnkita 1891 gold badge3 silver badges19 bronze badges 3
  • Try: getTotalCount(). Check what is the result. – Sujata Chanda Commented Nov 18, 2014 at 6:54
  • Can you try changing the url to '/asset.json' – Srinivas B Commented Nov 18, 2014 at 7:14
  • Put a break point in the store load function and check whether your store has real data or not.If data is there then it should return the count. – Sreek521 Commented Nov 18, 2014 at 7:56
Add a ment  | 

1 Answer 1

Reset to default 6

This is valid code. Here is working fiddle. You need to make sure you are calling store.getCount() after store has been loaded. store.load is async!! ... so doing store.load(); and then console.log(store.getCount()); is never going to print > 0. Use declarative or other kind of listener:

listeners: {
    load: function(store, records, successful) {
        Ext.Msg.alert('Store count A', 'count: ' + store.getCount());
    }
}

or

store.on('load', function(store, records, successful){
   Ext.Msg.alert('Store count A', 'count: ' + store.getCount());
});

store.load();
发布评论

评论列表(0)

  1. 暂无评论