2012-05-22 4 views
1

У меня есть модель, как это:Сенча сенсорный не чтение данных

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(), консоль показала "обновление", "updating1", и "updating3", а не какой-либо "updating2", то есть. записей не найдено. Интересно, где ошибка? Есть ли способ вывода jsonstring, который читается?

+0

попробовать 'console.log (это)' перед 'this.each', что вы получите? также, 'console.log (Ext.getStore ('Online'))'? –

+0

У меня есть TypeError: JSON.stringify не может сериализовать циклические структуры. для обоих –

ответ

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"); 
      } 
+0

Thx Alex, я перешел ко второму методу, но все равно не вдаваясь в каждый блок {} .... есть ли способ проверить, что было введено json input? –

+0

console.log (store) должен иметь store.data.items [i] .data – Alex

Смежные вопросы