【问题标题】:ExtJS 5: Understanding Ext.data.field.Field.reference config propertiesExtJS 5:了解 Ext.data.field.Field.reference 配置属性
【发布时间】:2015-05-25 02:01:45
【问题描述】:

我试图了解 Ext.data.field.Field.reference 配置的作用,因为名称有点尴尬。我想要做的是拥有如下所示的数据:

[{
  name: 'Mulder',
  state: 'Virginia',
  friends: [{
    name: 'Scully',
    type: 'partner'
  }, {
    name: 'Smoking Man',
    type: 'dad'
  }]
}]

并使用模型来消化它,所以我这样创建它们:

Ext.define('Agent', {
  extend: 'Ext.data.Model',
  fields: ['name', 'state']
});
Ext.define('Relationship', {
  extend: 'Ext.data.Model',
  fields: ['name', 'type', {
    name: 'agentId',
    reference: {
      type: 'Agent',
      inverse: {
        role: 'friends'
      }
    }
  }]
});

我在关系模型上将 inverse 作为对象的原因是因为 this thread... 基本上,我希望我的模型命名与检索到的数据不同,所以我想要它而不是 Friend 模型被称为Relationship 并在数据中引用friends,但所有这些都没有记录。在我解决了这个问题之后,我创建了我的商店 (test Fiddle here):

Ext.create('Ext.data.Store', {
  model: 'Agent',
  autoLoad: true,
  proxy: {
    type: 'ajax',
    url: 'data3.json',
    reader: {
      type: 'json'
    }
  },
  listeners: {
    load: function(store) {
      // outputs friends as a function and getFriends as null
      console.log('Agent', store.first().friends, store.first().getFriends);
    }
  }
});

所以我想要做的是让我的朋友的getter 方法被称为getFriends 而不是friends,但是通过查看文档,我不完全确定我使用哪个配置来指定它。我已经尝试了那些看起来很可能的候选人(关联、角色和反向)。在这一点上,我很确定这是我在 inverse 对象中设置的属性,但鉴于没有记录,我不知道。

以前,在第 4 版中,我们有 hasMany 关联,但这似乎被推到一边/没有记录以代替 reference 方式。我们将通过将其添加到Agent 模型来完成我想要做的事情(请参阅Fiddle 的工作示例):

hasMany: [{
  model: 'RelationshipHasMany',
  associationKey: 'friends',
  name: 'getFriends'
}]

有人对如何命名我的吸气剂有任何见解吗?也许您还可以解释一下属性associationrole 的作用。

【问题讨论】:

    标签: extjs extjs5


    【解决方案1】:

    你想使用getterName:

    inverse: {
        role: 'friends',
        getterName: 'getFriends'
    }
    

    【讨论】:

    • 太棒了,是的,我也想通了……谢谢埃文!这是文档中的任何地方,还是正在处理的事情?另外,hasMany 会消失吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 2011-10-01
    • 2015-07-27
    • 2015-10-27
    • 2016-07-25
    相关资源
    最近更新 更多