【问题标题】:ExtJS: store loaded, records in form but not in fieldsExtJS:存储已加载,记录在表单中但不在字段中
【发布时间】:2012-09-23 17:22:39
【问题描述】:

我一开始就在为我的申请苦苦挣扎。

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

我已经尝试过数字字段、文本字段以及将带有 ' 的数据更改为没有 ' 和混合......似乎没有任何帮助。

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

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

【问题讨论】:

  • 好的,看起来“Ext.widget”创建了一个带有新表单的新小部件,但是在更改后(在上面的源代码中更新)它仍然不起作用。

标签: javascript forms extjs store


【解决方案1】:

我认为您的问题是您需要将模型记录传递给 loadRecord 方法而不是基础数据。所以尝试将第 3 行更改为

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

附带说明一下,加载整个商店只是为了获得一条记录有点奇怪。 您可能想探索Model.load( id, {callback config} ) 加载您需要的确切记录的方式。

【讨论】:

猜你喜欢
  • 2013-07-17
  • 2019-08-10
  • 2012-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多