【问题标题】:ExtJS 5: Parent model convert dependency on Child associationExtJS 5:父模型转换对子关联的依赖
【发布时间】:2016-05-24 06:15:18
【问题描述】:

我有 2 个模型……一个父模型和一个子模型……一个父模型有许多子模型。在 Child 中,我有一个使用其数据创建的字段(在此示例中,它只是转换并返回 1)。在父级中,我进行了两次转换……一个使用它的数据,第二个取决于子级的数据。但是,在创建父模型时,子关联似乎还没有完全烘焙,但是当我用新值更新父模型时,子关联就在那里。

基本上,我想知道的是,当您看到 Total2 控制台触发时,我希望填充 Test。在父级中使用我的转换函数之前,如何强制读取关联?理想情况下,如果 Child 发生变化,Parent 中的依赖转换函数将自动触发......我意识到这很可能是不可能的,但这将是一个令人难以置信的额外奖励。

这是我的example

app.js

Ext.application({
    name : 'Fiddle',

    launch : function() {
        var store = Ext.create('Ext.data.Store', {
            model: 'Namespace.model.Parent',
            autoLoad: true,
            listeners: {
                load: onLoadStore
            }
        });
        var grid = Ext.create('Ext.grid.Panel', {
            title: 'associations',
            store: store,
            renderTo: Ext.getBody(),
            columns: [{
                text: 'Primary Key',
                dataIndex: 'PrimaryKey'
            }]
        });
        function onLoadStore(store, records, successful, eOpts) {
            var firstGroups = store.first().getGroupsStore();
            console.log('before', firstGroups.getCount(), firstGroups.isLoaded());
            firstGroups.first().set('GroupName', 'blah');
            store.first().set('blank', 1)
        }
    }
});

家长

Ext.define('Namespace.model.Parent', {
  extend: 'Ext.data.Model',
  alias: 'model.parent',
  requires: [
    'Ext.data.field.Integer',
    'Ext.data.field.String',
    'Namespace.model.Child'
  ],

  idProperty: "PrimaryKey",
  fields: [{
    type: 'string',
    name: 'PrimaryKey',
    critical: true
  }, {
    type: 'string',
    name: 'Title'
  }, {
      type: 'int',
      name: 'Rating',
      defaultValue: 2
  }, {
      type: 'int',
      name: 'Total',
      depends: ['Rating'],
      convert: function(value, record) {
          console.log('Total', record)
          return record.get('Rating') * 2;
      }
  }, {
      name: 'Total2',
      type: 'int',
      depends: ['groupsMapping', 'blank'],
      // depends on child's Test property
      convert: function(value, record) {
          var groupsMapping = record.get('groupsMapping');
          if (groupsMapping) {
              for (var i = 0; i < groupsMapping.length; i++) {
                  console.log('Total2', groupsMapping[i].GroupName, groupsMapping[i].Test);
              }
          }
          return 0;
      }
  }, {
      name: 'groupsMapping',
      type: 'auto',
      mapping: 'Groups'
  }, {
      name: 'blank',
      type: 'int'
  }],

    hasMany: [{
        model: 'Namespace.model.Child',
        associationKey: 'Groups',
        name: 'getGroupsStore'
    }],

  proxy: {
    type: 'ajax',
    url: 'data1.json'
  }
});

儿童

Ext.define('Namespace.model.Child', {
    extend: 'Ext.data.Model',
    alias: 'model.child',

    fields: [{
        type: 'int',
        name: 'GroupInt',
        critical: true
    }, {
        type: 'string',
        name: 'GroupName'
    }, {
        name: 'Test',
        type: 'int',
        depends: ['GroupName'],
        convert: function() {
            console.log('Test, parent depends on this')
            return 1;
        }
    }],
    proxy: {
        type: 'memory'
    }
});

【问题讨论】:

    标签: javascript extjs extjs5


    【解决方案1】:

    我做了一些类似于 qdev 的事情,除了我在实际协会的商店上收听。不过,构造函数有些奇怪......如果我没有把那个超时放在那里,那么我会在尝试访问存储加载中的第一条记录时出错......可能是一个框架错误,或者我我做了一些可怕的事情。重要的代码是构造函数和计算方法...在应用程序中,我在更改子数据时只有 1 秒超时...您可以看到网格的行数据从 3 变为 102。这是更新的 example

    constructor: function(config) {
        this.callParent(arguments);
        var me = this;
        setTimeout(function() {
            var groupsStore = me.getGroupsStore();
            if (groupsStore) {
                groupsStore.on('update', me.calculateChanges, me);
                groupsStore.on('datachanged', me.calculateChanges, me);
            }
            me.calculateChanges();
        }, 1);
    },
    
    calculateChanges: function() {
        console.log('calculating');
        var groupsStore = this.getGroupsStore();
        if (groupsStore) {
            var total2 = 0;
            groupsStore.each(function(group) {
                total2 += group.get('Test');
            });
            this.set('Total2', total2);
        }
    },
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,我花了一些时间才意识到关联在转换时不可用

      就像在您的示例中一样,我将子记录与父记录(嵌套)一起发送,并使用子模型和父字段子数据“手动”构建了一个假商店,以便为转换/计算进行数学运算,这成功了:

      {
        name: 'validationScore',
        depends: ['childRecords'],
        type: 'number',
        persist: false,
        convertOnSet: true,
        convert: function(val, rec){
          // here we have access only to direct record field, and not the association association !
      
          // we can use data directly in the calculation
          children = rec.get('childRecords');
      
          // or build a fake store in order to easily parse them
          children = Ext.create('Ext.data.Store', {
            model: 'Namespace.model.ChildModel',
            data: rec.get('childRecords')
          })
      
          // ... calculate whatever
      
          return result;
        }
      }
      

      但现在我的“新”挑战是找出一个(好的)解决方案来根据子记录的变化更新转换后的值...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-25
        • 2021-05-08
        • 1970-01-01
        • 2011-07-27
        • 2018-05-17
        • 1970-01-01
        相关资源
        最近更新 更多