【问题标题】:JSON Store can't load PHP data - EXTJS 4JSON 存储无法加载 PHP 数据 - EXTJS 4
【发布时间】:2013-02-12 10:46:28
【问题描述】:
Ext.define('GoogleMarkerModel', {
        extend: 'Ext.data.Model',
        fields: [
        {name: 'ID',    type: 'int'},
        {name: 'Locating',    type: 'int'},
        {name: 'MainPower',    type: 'int'},
        {name: 'Acc',    type: 'int'},
        {name: 'PowerOff',    type: 'int'},
        {name: 'Alarm',    type: 'int'},
        {name: 'Speed',    type: 'int'},
        {name: 'Direction',    type: 'int'},
        {name: 'Latitude',    type: 'float'},
        {name: 'Longitude',    type: 'float'},
        {name: 'DateTime',    type: 'date'},
        {name: 'MainID',    type: 'int'},
        {name: 'IOState',    type: 'int'},
        {name: 'OilState',    type: 'int'}]
    });

    var MarkerStore = Ext.create('Ext.data.JsonStore', {
        model: 'GoogleMarkerModel',
        autoLoad: true,
        proxy: {
            type: 'ajax',
            url: 'get-googlemarker.php',
            baseParams: {  //here you can define params you want to be sent on each request from this store
                        mainid: 'value1'
                        },
            reader: {
                type: 'json',
                idProperty:'MainID',
            }

        }
    });

我用来调用 MarkerStore 并传递值为 1 的参数 mainid 的代码

MarkerStore.load({
                        params: {  //here you can define params on 'per request' basis
                                mainid: 1,
                                }
                        })

当我使用网络浏览器获取-googlemarker.php 并且 mainid=1 将返回这些值

http://localhost/GPS/examples/tabs/get-googlemarker.php?mainid=1

[{"ID":"1808","Locating":"1","MainPower":"0","Acc":"1","PowerOff":"1","Alarm":"128","Speed":"0","Direction":"293","Latitude":"5.391788482666016","Longitude":"100.29693603515625","DateTime":"2013-02-19 15:44:36","MainID":"1","IOState":"0","OilState":"0"}]

但我试图列出所有数据,不幸的是 JSON 存储为空,我怀疑数据未存储在 MarkerStore 中,下面的代码正在尝试列出数据,但控制台 FireBug 上没有任何内容。

MarkerStore.each( function (model) {
    console.log( model.get('MainID') ); 
    }); 

有什么想法吗?

【问题讨论】:

    标签: javascript ajax extjs extjs4 store


    【解决方案1】:

    尝试从商店中删除 autoload = true 属性。

    【讨论】:

      【解决方案2】:
      MarkerStore.on('load', function(store, records) {
          for (var i = 0; i < records.length; i++) {
          console.log(records[i].get('Latitude'));
          lati = records[i].get('Latitude'); 
          };
      });
      

      应该使用此代码,然后将在控制台上打印数据。以上问题打印存储数据代码错误,实际数据成功保存在JSON存储中

      【讨论】:

        【解决方案3】:

        您的代码有效,但主要问题是当您调用 MarkerStore.each 时,此时商店为空(Ajax 是异步的)。您的脚本与数据的 ajax 一样快。如果您使用以下简单的 sn -p,您将看到您的“mainID”。

        setTimeout(function(){
                MarkerStore.each( function (model) {
                        console.log( model.get('MainID') );
                });     
            }, 1500, MarkerStore);
        

        【讨论】:

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