2012-10-02 38 views
4

我在開始時一直在努力應對我的應用程序。ExtJS:存儲加載,記錄形式但不在字段中

this.getScoresStore().on('load', function(score, records) { 
    var view = Ext.getCmp('scoreView'); 
    view.down('form').loadRecord(records[0].data); 
    console.log(view.down('form').getRecord()); 
    console.log(view.down('form').getValues()); 
}); 

存儲加載後,我將記錄添加到表單中。控制檯說它已被添加,但是這些字段保持空白。

Object { playerOne="301", playerTwo="301" } 
Object { playerOne="", playerTwo="" } 

任何人都有想法什麼可能是錯的?

控制器:

Ext.define('Darts.controller.Scores', { 
    extend: 'Ext.app.Controller', 

    views: [ 
     'score.View', 
     'score.Hit' 
    ], 

    stores: [ 
     'Scores' 
    ], 

    models: [ 
     'Score' 
    ], 

    init: function() { 
     this.getScoresStore().on('load', function(score, records) { 
      var view = Ext.getCmp('scoreView'); 
      view.down('form').loadRecord(records[0].data); 
      console.log(view.down('form').getRecord()); 
      console.log(view.down('form').getValues()); 
     }); 

     this.control({ 
      'scoreView' : { 
       afterrender: this.formRendered 
      } 
     }); 
    }, 

    formRendered: function(obj) { 
     console.log(obj.down('form').getRecord()); 
     console.log('form rendered'); 
    } 
}); 

瀏覽:

Ext.define('Darts.view.score.Hit' ,{ 
    extend: 'Ext.panel.Panel', 
    alias : 'widget.scoreHit', 

    title : 'Hits', 
    score : 'Scores', 

    initComponent: function() { 
     this.items = [ 
      { 
       xtype: 'form', 
       items: [ 
        { 
         xtype: 'textfield', 
         name : 'playerTwo', 
         fieldLabel: 'Player 1' 
        } 
       ] 
      } 
     ]; 

     this.callParent(arguments); 
    } 
}); 

Ext.define('Darts.view.score.View' ,{ 
    extend: 'Ext.panel.Panel', 
    alias : 'widget.scoreView', 
    id : 'scoreView', 

    title : 'Player Scores', 
    score : 'Scores', 

    initComponent: function() { 
     this.items = [ 
      { 
       xtype: 'form', 
       items: [ 
        { 
         xtype: 'numberfield', 
         name : 'playerOne', 
         fieldLabel: 'Player 1' 
        }, { 
         xtype: 'textfield', 
         name : 'playerTwo', 
         fieldLabel: 'Player 2' 
        } 
       ] 
      } 
     ]; 

     this.buttons = [ 
      { 
       text: 'Start Game', 
       action: 'start' 
      } 
     ]; 

     this.callParent(arguments); 
    } 
}); 

商店

Ext.define('Darts.store.Scores', { 
    extend: 'Ext.data.Store', 
    model : 'Darts.model.Score', 
    autoLoad: true, 

    proxy: { 
     type: 'ajax', 
     api: { 
      read: 'data/scores.json', 
      update: 'data/updateScores.json' 
     }, 
     reader: { 
      type: 'json', 
      root: 'scores', 
      successProperty: 'success' 
     } 
    } 
}); 

型號:

Ext.define('Darts.model.Score', { 
    extend: 'Ext.data.Model', 
    fields: ['playerOne', 'playerTwo'] 
}); 

數據:

{ 
    success: true, 
    scores: [ 
     {id: 1, playerOne: '301', playerTwo: '301'} 
    ] 
} 

我試過numberfields,文本框以及改變與數據FOM「而不」和混合....似乎沒有任何幫助我。商店加載之前(測試輸出仍處於代碼)

我真的離開這裏的想法和我見過很多話題

的字段呈現,但沒有適合我的問題還是解決了我的問題。表單域始終保持爲空。

+0

好吧,看起來像「Ext.widget」創建一個新窗體的新窗口小部件,但是在更改後(更新源代碼上面)它仍然不起作用。 –

回答

4

我認爲你的問題是你需要傳遞一個模型記錄到loadRecord方法而不是底層數據。因此,嘗試將第3行更改爲

view.down('form')。loadRecord(records [0]);

作爲一個附註,加載整個商店只是爲了獲得單個記錄有點奇怪。 你可能想要探索加載你需要的確切記錄的方式。

+0

謝謝,就是這樣!這就是魔術。 謝謝你的注意事項,我會檢查一下! –

+0

嗨@dbrin你可以更新這個答案,並把例子?我不知道什麼是回調配置 –

+0

轉到此頁面,查找加載方法。 http://docs.sencha.com/extjs/4.0.7/?mobile=/api/Ext.data.Model – dbrin

相關問題