Sencha Touch无法读取数据

时间:2012-05-22 15:50:35

标签: extjs sencha-touch sencha-touch-2

我的模型如下:

Ext.define("NotesApp.model.Note", {
    extend: "Ext.data.Model",
    config: {
        idProperty: 'id',
        fields: [
            { name: 'id', type: 'int' },
            { name: 'dateCreated', type: 'date', dateFormat: 'c' },
            { name: 'question', type: 'string' },
            { name: 'answer', type: 'string' },
            { name: 'type', type: 'int'},
            { name: 'author', type: 'int'}
        ],
        validations: [
            { type: 'presence', field: 'id' },
            { type: 'presence', field: 'dateCreated' },
            { type: 'presence', field: 'question', message: 'Please enter a question for this card.' }
        ]
    }
});

和我的服务器页面(qa.php)输出:

{"results":[{"id":"1","dateCreated":"2012-05-01","question":"asdf?","answer":"fdsa","type":"0","author":"0"},{"id":"2","dateCreated":"2012-05-01","question":"qwer?","answer":"rewq","type":"0","author":"0"}]}

这是我在商店中的代码:

Ext.define("NotesApp.store.Online", {
    extend: "Ext.data.Store",
           config: {
           model: 'NotesApp.model.Note',
           storeId: 'Online',
           proxy: {
               type: 'jsonp',
               url: 'http://xxxxxxxx.com/qa.php',
               reader: {
                   type: 'json',
                   rootProperty: 'results'
               }
           },
           autoLoad: false,
           listeners: {
               load: function() {   
                   console.log("updating");

                   Ext.getStore('Notes').getProxy().clear();
                   console.log("updating1");

                   // Loop through records and fill the offline store                       

                   this.each(function(record) {
                             console.log("updating2");
                             Ext.getStore('Notes').add(record.data);

                   });

                   // Sync the offline store
                   Ext.getStore('Notes').sync();
                   console.log("updating3");
                   // Remove data from online store
                   this.removeAll();
                    console.log("updated");
               }

           },
           fields: [
                    {
                    name: 'id'
                    },
                    {
                    name: 'dateCreated'
                    },
                    {
                    name: 'question'
                    },
                    {
                    name: 'answer'
                    },
                    {
                    name: 'type'                    
                    },
                    {
                    name: 'author'
                    }
                    ]
           }
});

但是当我打电话给Ext.getStore('Online').load()时,控制台显示“正在更新”,“正在更新1”和“正在更新3”,而不是“更新2”,即。没有找到记录。我想知道错误在哪里?有没有办法输出读入的jsonstring?

1 个答案:

答案 0 :(得分:0)

这是一个不同的范围

listeners: {
           load: function() {   
               console.log("updating");

               Ext.getStore('Notes').getProxy().clear();
               console.log("updating1");

               // Loop through records and fill the offline store                       

               this.each(function(record) {
                         console.log("updating2");
                         Ext.getStore('Notes').add(record.data);

               });

               // Sync the offline store
               Ext.getStore('Notes').sync();
               console.log("updating3");
               // Remove data from online store
               this.removeAll();
                console.log("updated");
           },
this//adds scope

listeners: {
           load: function(store,record,options) {   
               console.log("updating");

               Ext.getStore('Notes').getProxy().clear();
               console.log("updating1");

               // Loop through records and fill the offline store                       

               store.each(function(record) {
                         console.log("record"); //wahei records!
                         Ext.getStore('Notes').add(record.data);

               });

               // Sync the offline store
               Ext.getStore('Notes').sync();
               console.log("updating3");
               // Remove data from online store
               this.removeAll();
                console.log("updated");
           }