EXTJS7 publishes将配置属性发布到viewModel

小编:管理员 451阅读 2022.09.07

在组件声明使用publishes
Ext.define('myComponent', {
	extend: 'Ext.Component',
	xtype: 'my-component',
	// 配置属性,默认不支持组件直接绑定属性
	config: {
		prop1: null,
		...
	},
	publishes: {
		// 增加此项将配置属性发布到viewModel
		prop1: true
	}
});
复制在组件实例使用publishes
items:[{
	xtype: 'my-component',
	reference: 'mycomponent',
	publishes: ['prop1']
}, {
	type: 'textfield',
	bind: '{mycomponent.prop1}'
}],
viewModel:{...}
复制
  • 使用reference后viewModel会自动生成一项data字段‘mycomponent’,并包含组件通过publishes发布的属性
注意事项
  • 如果组件实例不存在reference属性,则publishes配置将被忽略
  • 如果组件内部包含viewModel,reference将引用映射到组件内部的viewModel中, 导致组件实例的owner.viewModel无法获取组件引用。 例如:前文源码案例中如果my-component定义中包含viewModel,则会在my-component.viewModel.data中创建引用 mycomponent: {prop1=xxx},而在外层的viewModel中不会创建引用
  • reference不可使用连字符 “-”,否则会导致虽然可以在viewModel中创建引用,但无法创建绑定的问题
items:[{
	xtype: 'my-component',
	reference: 'mycomponent-a',
	publishes: ['prop1']
}, {
	type: 'textfield',
	bind: '{mycomponent-a.prop1}'
}]
复制

此例中,虽然viewModel.data中有mycomponent-a: {prop1:xxx},但是textfield的绑定是无效的

关联标签: