2011-11-15 30 views
0

我正在學習Sencha,所以我有非常基礎的問題。 我想建立一個簡單的mvc應用程序,與控制器,視圖模型和商店。如何閱讀在Sencha Touch 2.0中從控制器寫入商店中的內嵌數據mvc

我有這個nmodel

Ext.define('rpc.model.Studenti', { 
Extend: 'Ext.data.Model', 
fields: [ 
    { name: 'cognome', type: 'string' }, 
    { name: 'previsione', type: 'string' } 
] 
}); 

此店(內聯數據)

Ext.define('rpc.store.Studenti', { 
model: 'rpc.model.Studenti', 
storeId: 'gruppoStore', 
autoLoad: true, 
data: [ 
    {cognome: 'uno', previsione: 'aaa'}, 
    {cognome: 'due', previsione: 'bbb'}, 
    {cognome: 'tre', previsione: 'ccc'} 
] 
}); 

控制器

Ext.define('rpc.controller.Home', { 
extend: 'Ext.app.Controller', 
stores: ['Studenti'], 
models: ['Studenti'], 
views: ['home.Fila','home.Griglia','home.Previsio'], 
store: 'gruppoStore', 
init: function() { 
    this.control({ 
     'griglia button': { 
      tap: this.faqualcosa 
     } 
    }); 
}, 
faqualcosa: function(button){ 
    ... 
    var gruppoStoreMgr=this.getStudentiStore(); 

    alert (gruppoStoreMgr.count()); 
    for (var key in gruppoStoreMgr) {   
     //alert (key); 
    } 
    alert (gruppoStoreMgr.storeId); 
    alert (gruppoStoreMgr.isInstance); 
    //alert (gruppoStoreMgr.data); 
    alert (gruppoStoreMgr.autoLoad); 
    for (var key in gruppoStoreMgr.data[0]) { 
     //alert ("0"+key); 
    } 
    for (var key in gruppoStoreMgr.data[1]) { 
     //alert ("1"+key); 
    } 
} 
}); 

請,什麼是功能faqualcosa訪問的正確方法到商店的數據? 我已經能夠像var實例一樣(確定它非常接近...)var gruppoStoreMgr = this.getStudentiStore();但我沒有記錄我在商店寫的數據(「uno」,「due」....)。如何獲取這些數據?如何在控制器中使用引用商店中真實數據的對象?

回答

2
this.getStudentiStore().data.items 

這是獲取數據的最直接方式。它返回模型記錄的數組。如果這就是你想要的。

for(var i = 0; i < this.geStudentiStore().getCount(); i++) { 
    var record = this.getStudentiStore().getAt(i) 
    console.log(record.get('cognome')); 
} 

這應該打印出所有的「cognomes」

+0

真正的問題是在存儲文件丟失。我錯過了延長店面的基礎。添加行擴展:'Ext.data.Store'我開始有事情在做。無論如何感謝您的答案stan229 –

相關問題