【问题标题】:ExtJS refresh grid when object in store is changed当存储中的对象更改时,ExtJS 刷新网格
【发布时间】:2021-08-17 15:09:02
【问题描述】:

我有一个包含对象的商店,我有一个带有这个商店的视图模型,我有一个网格,其中商店绑定到这个视图模型商店。

示例:(在我的情况下,对象要复杂得多)

const things = {car: 'Fiat', house: 'big'};

   Ext.define('MyApp.store.users', {
      extend  : 'Ext.data.Store',
      model   : 'MyApp.model.users',
      alias   : 'store.users',
      data    : [{
          name: 'john',
          things: things
      }]
   });

Ext.define('MyApp.model.users', {
   extend  : 'Ext.data.Model',
   alias   : 'model.users',
   fields: [{
      name : 'name'
   }, {
      name : 'things',
      type : 'boolean',
      calculate(d) { return isHouseBig(d.house) }
   }],
   proxy: {
      type : 'localstorage',
      id   : 'usb-devices'
   }
});

things.car = 'Ferrari';

那么,当我更改 house 的值时,如何让我的网格更新?

【问题讨论】:

    标签: javascript extjs viewmodel


    【解决方案1】:

    当 store 改变时,ExtJS 会自动刷新网格。当您的数据以 JSON 对象check documentation here 形式出现时,您在模型定义中使用了mapping 的问题。当您像这样设置字段时,如果您修改商店数据,网格将自动反映更改。

    检查下面的代码,在最后注释掉这两行,看看它们在执行时的区别。你可以找到这个简化的例子here as a working fiddle

    Ext.define('MyApp.model.users', {
       extend  : 'Ext.data.Model',
       fields: [{
          name : 'name'
       },{
          name : 'things',
       },{
          name : 'car',
          // so in every model `car` will represent `things.car`
          mapping : 'things.car',
       },{
          name : 'house',
          mapping : 'things.house',
       }],
    });
    
    Ext.define('MyApp.store.users', {
        extend  : 'Ext.data.Store',
        model   : 'MyApp.model.users',
        data    : [{
          name: 'john',
          things: {car: 'Fiat', house: 'big'}
        },{
          name: 'mary',
          things: {car: 'Opel', house: 'small'}
        }],
        proxy: {
          type: 'memory'
        }
    });
    
    const usersStore = Ext.create('MyApp.store.users');
    
    const usersGrid = Ext.create({
        xtype: 'grid',
        renderTo: Ext.getBody(),
        store: usersStore,
        width: 400,
        height: 200,
        title: 'Users',
        columns: [{
            text: 'name',
            dataIndex: 'name'
        },{
            text: 'car',
            dataIndex: 'car'
        },{
            text: 'house',
            dataIndex: 'house'
        }]
    });
    
    // uncomment these lines to see change
    
    //usersStore.getAt(1).set('car', 'newCar');
    //usersStore.getAt(1).set('house', 'newHouse');
    
    

    【讨论】:

    • 抱歉,我的问题不是很清楚。问题是,如果我在其他地方更新对象,它不会更新网格。请看你的分叉小提琴fiddle.sencha.com/#view/editor&fiddle/3ff7
    • 好的,我明白了。这不适用于 ExtJS。常量things1things2 将在创建存储时被代理使用,但存储不会包含对这些常量的引用,它们是单独的变量。
    • Store是models的集合,models里面有很多ExtJS添加的内部数据,你case的model字段值是在填充store的时候从常量里复制过来的,但是之后过着独立的生活.
    • things1.car = 'Ferrari之前给你的小提琴添加一个断点并检查手表中的局部变量,在usersStore.data.items下你会看到你的模型数组,你可以看到差异。
    • 这有效:usersStore.data.items[0].data.car = 'Ferrari'; usersStore.data.items[0].data.house = 'Huge';,但强烈推荐的方法是使用 ExtJS set 和其他功能,如答案。
    猜你喜欢
    • 2014-03-28
    • 2011-08-18
    • 1970-01-01
    • 2023-03-15
    • 2015-04-18
    • 2012-05-19
    • 1970-01-01
    • 2011-05-14
    • 2011-02-15
    相关资源
    最近更新 更多