我有兩個組件:一個面板和一個自定義文本字段。
該面板有一個視圖模型,我想將該視圖模型的值(稱爲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