【发布时间】:2016-11-25 04:54:09
【问题描述】:
我的 ExtJS 5 组件的 ViewModel 正在从祖先的 ViewModel 继承数据。
如何从孩子的角度更新父母的 ViewModel 数据?如果我尝试在childPanel ViewModel 上创建myProp 的新实例childPanel.getViewModel().set('myProp', 'foo');,而不是更新parentPanel 的VM。使用myProp 值的其他组件将不同于childPanel。
通过在子节点上创建本地myProp,当父节点的myProp 发生变化时,子节点不会随之改变,因为已断开关系。
我只想要 myProp 属性的 1 个实例,并且该值位于父级的 ViewModel 上。
要解决这个问题,我的子虚拟机似乎必须知道该属性是继承的还是存储在本地的,这要求子虚拟机知道应用程序的正确架构。它必须了解比我熟悉的更多的应用程序架构,从而显着地将子级与父级耦合。
示例展示了子创建panelTitle 的新实例而不是更新父实例:
https://fiddle.sencha.com/#fiddle/1e09
编辑:将按钮的点击事件移动到 ViewController
Ext.application({
name: 'Fiddle',
launch: function() {
Ext.define('ChildPanel', {
extend: 'Ext.panel.Panel',
alias: 'widget.childpanel',
controller: 'childpanelcontroller',
bind: {
title: '{panelTitle}'
},
// first panel has its own viewmodel, which is a child of the viewport's VM
viewModel: {
data: {
'btnText': 'Click to Change Title'
}
},
items: [{
xtype: 'button',
scale: 'large',
bind: {
text: '{btnText}'
},
listeners: {
'click': 'onButtonClick'
}
}]
});
Ext.define('ChildPanelController', {
extend: 'Ext.app.ViewController',
alias: 'controller.childpanelcontroller',
onButtonClick: function(btn) {
// updates first panel's VM
this.getViewModel().set('panelTitle', '<span style="color:red;">Now They Have Different Titles</span>');
debugger;
window.setTimeout(function() {
// by setting the first panel's VM instead of the viewport's,
// the first panel will not use viewport's VM for `panelTitle` property again
btn.up('viewport').getViewModel().set('panelTitle', '<span style="color:green;">And Are No Longer Using the Same VM Data</span>');
}, 500);
}
});
Ext.create('Ext.container.Viewport', {
viewModel: {
data: {
'panelTitle': 'Both Have the Same Title'
}
},
layout: 'hbox',
items: [{
xtype: 'childpanel',
flex: 1
}, {
// second panel uses the viewport's viewmodel
xtype: 'panel',
bind: {
title: '{panelTitle}'
},
flex: 1,
margin: '0 0 0 25px'
}]
});
}
});
【问题讨论】:
-
您希望孩子更改父母的属性,但您不希望孩子知道其与父母的关系?
-
孩子知道该属性存在,但不知道也不关心它的来源。该财产可以从其父母或其曾曾曾曾祖父母继承。如果它来自多个级别,孩子需要知道它需要跳转到正确的 VM (child.getViewModel().getParent().getParent().getParent()...) 才能更新它。
标签: extjs mvvm viewmodel extjs5