【问题标题】:sencha, Grid not update automatically when a store is updatedsencha,更新商店时网格不会自动更新
【发布时间】:2018-07-22 18:40:31
【问题描述】:

请考虑以下使用 extjs 6.5 现代编写的测试代码。

 Ext.define('myApp.store.Sections', {
     extend: 'Ext.data.Store',
     storeId: 'Sections',
     alias: 'store.sections',

     fields: ['name', 'event'],

     data: {
         items: [{
             name: 'blah',
             event: 'a'
         }, {
             name: 'hello',
             event: 'a'
         }, {
             name: 'world',
             event: 'a'
         }, {
             name: 'foo',
             event: 'b'
         }, {
             name: 'bar',
             event: 'b'
         }]
     },

     proxy: {
         type: 'memory',
         reader: {
             type: 'json',
             rootProperty: 'items'
         }
     }
 });
 Ext.create({
     xtype: 'container',
     title: 'Panel Title',
     iconCls: 'x-fa fa-html5',
     height: 400,
     width: 400,
     fullscreen: true,

     layout: {
         type: 'vbox',
         align: 'stretch'
     },
     items: [{
         xtype: 'grid',
         name: 'master',
         store: {
             type: 'sections',
         },
         layout: 'fit',
         flex: 1,
         plugins: {
             gridcellediting: {
                 selectOnEdit: true,
                 triggerEvent: 'tap',
             }
         },
         columns: [{
             text: 'name',
             dataIndex: 'name',
             flex: 1,
             editor: {
                 xtype: 'textfield',
             }
         }]
     }, {
         xtype: 'grid',
         name: 'firstslave',
         store: {
             type: 'sections',
         },
         layout: 'fit',
         flex: 1,
         columns: [{
             text: 'name',
             dataIndex: 'name',
             flex: 1
         }]
     }, {
         xtype: 'combobox',
         name: 'secondslave',
         displayField: 'name',
         valueField: 'name',
         store: {
             type: 'sections'
         }
     }]
 });

可以通过第一个网格修改商店条目。如果以这种方式修改存储,则更改在组合框内可见(第二个从属)。

但是第二个网格没有反映这些变化,那里的项目保持不变,因此网格视图与基础数据不同步。

为什么会这样?可以预防吗?

编辑:我注意到,如果我重新排序网格(通过列菜单),项目就会更新。

【问题讨论】:

    标签: gridview extjs data-binding datastore extjs6-modern


    【解决方案1】:

    当你为你所有组件使用store

    store: {
      type: 'sections'
    }
    

    因此,在这种情况下,每个组件都有您的 sections 存储的新实例。这就是您的更改没有反映的原因。


    你可以通过使用 2 方式来达到你想要的结果

    1. 通过使用ViewModelbinding 配置

    2. 您可以直接将 storeId 分配给您的组件

      store:'sections' //In that case you store should be created
      

    在这个FIDDLE中,我使用ViewModelbinding创建了演示。

    Ext.application({
        name: 'Fiddle',
    
        launch: function () {
    
            Ext.define('myApp.store.Sections', {
                extend: 'Ext.data.Store',
                storeId: 'Sections',
                alias: 'store.sections',
    
                fields: ['name', 'event'],
    
                data: {
                    items: [{
                        name: 'blah',
                        event: 'a'
                    }, {
                        name: 'hello',
                        event: 'a'
                    }, {
                        name: 'world',
                        event: 'a'
                    }, {
                        name: 'foo',
                        event: 'b'
                    }, {
                        name: 'bar',
                        event: 'b'
                    }]
                },
    
                proxy: {
                    type: 'memory',
                    reader: {
                        type: 'json',
                        rootProperty: 'items'
                    }
                }
            });
    
            Ext.define('MainModel', {
                extend: 'Ext.app.ViewModel',
    
                alias: 'viewmodel.main',
    
                stores: {
                    myStore: {
                        type: 'sections'
                    }
                }
            });
    
            Ext.create({
                xtype: 'container',
                title: 'Panel Title',
                iconCls: 'x-fa fa-html5',
                //height: 400,
                fullscreen: true,
    
                viewModel: {
                    type: 'main'
                },
    
                layout: {
                    type: 'vbox',
                    align: 'stretch'
                },
                items: [{
                    xtype: 'grid',
                    title: 'Grid 1',
                    name: 'master',
                    bind: '{myStore}',
                    layout: 'fit',
                    flex: 1,
                    plugins: {
                        gridcellediting: {
                            selectOnEdit: true,
                            triggerEvent: 'tap'
                        }
                    },
                    columns: [{
                        text: 'name',
                        dataIndex: 'name',
                        flex: 1,
                        editor: {
                            xtype: 'textfield'
                        }
    
                    }]
                }, {
                    xtype: 'grid',
                    title: 'Grid 2',
                    name: 'firstslave',
                    bind: '{myStore}',
                    layout: 'fit',
                    flex: 1,
                    columns: [{
                        text: 'name',
                        dataIndex: 'name',
                        flex: 1
                    }]
                }, {
                    xtype: 'panel',
                    title: 'Combo panel',
                    items: [{
                        xtype: 'combobox',
                        name: 'secondslave',
                        displayField: 'name',
                        valueField: 'name',
                        bind: {
                            store: '{myStore}'
                        },
                        margin:'0 0 30 0'
                    }]
                }]
            });
        }
    });
    

    【讨论】:

    • 这(viewmodel: { type: x }})不是也意味着每个组件都有自己的视图模型吗?因此它自己的商店副本?
    • viewmodel: { type: x }} 这只是为您的组件定义视图模型的类型。它没有创建新实例。视图模型是在您使用过的整个应用程序中放置和反映一次概念更改
    • 我已经提到了另一种方法,即提供商店 ID,您也可以使用它。
    • 哦,我并不是说解决方案不好,我只是想了解为什么 `store: {type: x} 会为每个组件创建一个新存储,但相同的语法却没有为每个视图模型执行此操作。
    • 没有人用这种方式来定义视图模型。但是对于商店,您有两种方法来分配别名或商店 ID
    猜你喜欢
    • 2011-07-30
    • 2011-06-07
    • 2017-11-09
    • 1970-01-01
    • 2013-09-22
    • 2019-07-23
    • 2016-01-19
    • 2018-06-29
    • 2018-04-09
    相关资源
    最近更新 更多