ExtJS:存储已加载,记录在表单中但不在字段中

时间:2012-10-02 19:58:15

标签: javascript forms extjs store

我一开始就在努力应用我的应用程序。

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'}
    ]
}

我已尝试使用数字字段,文本字段以及将数据更改为“to without”和“混合......”似乎没有任何帮助。

在加载存储之前渲染字段(测试输出仍在代码中)

我真的没有想法,我看过很多主题,但没有一个适合我的问题或解决我的问题。表单字段始终保持空白。

1 个答案:

答案 0 :(得分:4)

我认为您的问题是您需要将Model记录传递给loadRecord方法而不是基础数据。因此,请尝试将第3行更改为

  。

view.down( '形式')loadRecord(记录[0]);

作为旁注,加载整个商店只是为了获得单个记录有点奇怪。 您可能希望探索Model.load( id, {callback config} )加载所需的精确记录的方法。