如何使用ExtJS商店访问相关模型中的数据?

时间:2015-02-25 10:57:12

标签: javascript json extjs extjs4.2

我在一个页面中有几个组件,主要是自制的。每个人都有自己的商店+模特+视图。出于性能原因,我想向后端发出一个AJAX请求,以便立即获取所有数据,然后浏览每个数据部分并将这些部分分配给相应组件的存储。 但似乎我无法获取/阅读这些部分。 我试图按照Extjs4: How to share data between multiple stores or models?中的提示进行操作,但没有取得任何成功。

后端返回的JSON数据如下:

{
  "success":true,
  "results":8,
  "data":[{
    "bv01":{
      "success":true,
      "count":1,
      "data":[{
        "active_learner_count":0,
        "connected_learner_count":0,
        "enrolled_learner_count":123999,
        "registered_learner_count":120098
      }]
    }
  },{
    "bv02":{
      "success":true,
      "count":1,
      "data":[{
          // Blabla
      }]
    }
  },{ 
    // 6 other embedded responses
  }]
}

以下是我使用的商店/模型的片段:

最里面的模型,包含链接到模板的数据(此处未显示):

Ext.define('Ckls.module.Graphs.Square.model.Learner', {
  extend: 'Ext.data.Model',
  fields: [
    {name: 'hugeRectCount', type: 'int', mapping: 'registered_learner_count'},
    {name: 'bigRectCount', type: 'int', mapping: 'enrolled_learner_count'},
    {name: 'mediumRectCount', type: 'int', mapping: 'active_learner_count'},
    {name: 'smallRectCount', type: 'int', mapping: 'connected_learner_count'}
  ]
});

组合模型:

Ext.define('Reporting.Reporting.model.Kpi.Bv01', {
  extend: 'Ext.data.Model',
  fields:[{
    name: "success",
    type: "auto"
  },{
    name: "count",
    type: "auto"
  }],
  hasMany: {
    model: 'Ckls.module.Graphs.Square.model.Learner',
    name: 'data'
  },
  belongsTo: 'Reporting.Reporting.model.Block'
});

一次通话型号:

Ext.define('Reporting.Reporting.model.Block', {
  extend: 'Ext.data.Model',
  hasOne: [{
    model: 'Reporting.Reporting.model.Kpi.Bv01',
    name : "bv01"
  },{
    model: 'Reporting.Reporting.model.Kpi.Bv02',
    name : "bv02"
  },{
    ... // several extra models inclusions come here
  }]
});

单呼叫初始化的商店定义:

Ext.define('Reporting.Reporting.store.Blocks', {
  extend: 'Ext.data.Store',
  model: 'Reporting.Reporting.model.Block',
  proxy: {
    type  : 'ajax',
    reader: {
      type: 'json',
      root: 'data'
    },
    url: '/some_url.php?some_parameter=some_value'
  }
});

控制器:

Ext.define('Reporting.Reporting.controller.Reporting', {
  extend: 'Ext.app.Controller',
  init: function() {
    // 'activities' is my main view
    this.control({
      'activities': {
        afterrender: this.initAllStores
    });
  },
  initAllStores: function() {
    var blockStore = Ext.create('Reporting.Reporting.store.Blocks');
    var blockStoreListeners = blockStore.on('load', this.blockStoreLoaded, this);
    blockStore.load();
  },
  blockStoreLoaded: function(store, records, successful, eOpts) {
    // As far as I can tell from the documentation, this should 
    // return the data as read in the sub-model but it throws an
    // "undefined" error
    var bv01results = store.data.getBbv01();
    Ext.log({msg:"bv01results", dump: bv01results});
    var bv01store = someFunctionToGetThatBv01Store();
    bv01store.loadRawData(bv01results);
  }
}

我不知道如何访问内部数据然后将它们加载到bv01存储中。在Chrome中,控制台会显示错误Uncaught TypeError: undefined is not a function

修改

我已经为模型添加了一个内存代理(如https://stackoverflow.com/a/19361382/2732205所示):

Ext.define('Reporting.Reporting.model.Kpi.Bv01', {
  // ....
  proxy: {
    type: 'memory',
    reader: {
      type: 'json'
    }
  }
}

修复了控制器中的函数调用以获取数据:

Ext.define('Reporting.Reporting.controller.Reporting', {
  // ....
  blockStoreLoaded: function(store, records, successful, eOpts) {
    var record0 = store.getAt(0);
    Ext.log({msg:"record0", dump: record0});

    // It seems that it's now trying to load the "bv01" from the
    // network instead of just reading it from the record
    var bv01results = record0.getBv01();
    Ext.log({msg:"bv01results", dump: bv01results});

    var bv01store = someFunctionToGetThatBv01Store();
    bv01store.loadRawData(bv01results);
  }
}

现在控制台会显示Uncaught TypeError: Cannot read property 'hasId' of undefined。从我所做的搜索中,当尝试从网络中读取数据时,似乎会出现这种情况。

我有点进一步,但我仍然有被卡住的感觉......

0 个答案:

没有答案