我有两个组件:一个面板和一个自定义文本字段。
该面板有一个视图模型,我想将该视图模型的值(称为testData
)绑定到自定义文本字段的属性(称为test
)。
这工作正常......基本上。
但是,当文本字段的test
属性发生更改时,面板视图模型中的testData
不会相应更新。我的意思是,当子元素(文本字段)的test
属性被修改时,面板视图模型的testData
属性应该包含与test
中相同的值,就像普通的双向绑定一样。ExtJs父子组件之间的双向绑定
我不知道我在做什么错,但这里是我试过至今: 所有的https://fiddle.sencha.com/#fiddle/20pu&view/editor
Ext.define('MyMain', {
extend: 'Ext.panel.Panel',
alias: 'widget.main',
width: '100%',
bodyPadding: 10,
viewModel: {
data: {
testData: 'Example Data'
}
},
bind: {
title: '{testData}'
},
items: {
xtype: 'myField',
bind: {
test: '{testData}'
}
}
})
Ext.define('MyField', {
extend: 'Ext.form.field.Text',
alias: 'widget.myField',
fieldLabel: 'Data',
width: '100%',
config: {
test: null // when test is changed, it should also affect the {testData} bind of the main component, causing the title to change
},
setTest(value) {
this.test = value + ' modified!' // because of the bind, this /should/ automatically get appied to the viewmodel's `testData` and thus to the panel title
this.setValue(this.test) // whenever the `test` property is changed, we write the contents to the value of the text field (just to visualize the `test` property).
// But as you can see, the panel title will still just say `Example Data` and not `Example Data modified!` as it should.
},
getTest(){
return this.test
}
})
Ext.application({
name : 'Fiddle',
launch : function() {
Ext.create('Ext.container.Viewport', {
items: [{
xtype: 'main'
}]
})
}
})
首先将'config'作为默认值,然后使用'test'参数创建组件,该参数将覆盖'test'中的默认值。 – Edwin
配置值只能从父组件设置一次并不重要。我的主要问题是更改测试值不会影响父组件的viewmodel属性。任何想法如何可以解决? – Forivin
你没有任何'测试'在父... ..只是一个'testData' – Edwin