gpt4 book ai didi

javascript - 双向绑定(bind)不更新 View 模型

转载 作者:行者123 更新时间:2023-12-02 05:16:42 33 4
gpt4 key购买 nike

我正在尝试在 Ext JS 6.0.2 组件中使用双向绑定(bind)。但是,据我所知,它并没有像我期望的那样工作,或者根本没有工作。我创建了一个最小示例:

Ext.define('MyComponentController', {
extend: 'Ext.app.ViewController',
alias: 'controller.mycomponent'
});

Ext.define('MyComponentModel', {
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.mycomponent'
});

Ext.define('MyComponent', {
extend: 'Ext.Component',
xtype: 'mycomponent',

viewModel: {
type: 'mycomponent'
},
controller: 'mycomponent',

config: {
thing: 'a defualt value'
},
bind: {
thing: '{thing}'
},
twoWayBindable: 'thing'
});

var myComponent = Ext.create('MyComponent', {
thing: 'a new value'
});

// Use setTimeout to give bindings time to update.
setTimeout(function() {
console.log(myComponent.getViewModel().get('thing'));
}, 1000);

我这里还有一个 Sencha fiddle :https://fiddle.sencha.com/#fiddle/1efk

我期望运行此代码的是看到 一个新值 记录到控制台。相反,我得到 null。即使我设置了 bindtwoWayBindable,我的 View 上设置的值也没有发布到我的 View 模型。我是否误解了如何使用双向绑定(bind)?

最佳答案

看看这个例子:Fiddle

这是您示例的一个分支。我添加了一些东西:

  • Ext.application 带有 launch 方法,并将 myComponent 的创建放在此处。
  • myComponent 呈现给 Ext.getBody()
  • thing 配置的更新方法,它在更改时记录其值。
  • 一个ViewModel binding ,它在更改时记录其值。

Ext 在初始化 View 模型之前不会设置绑定(bind)。这不会发生,直到 View 本身被初始化。这就是为什么需要渲染它的原因。 正如 Mitchell Simoens 正确指出的那样,实例化一个 View 将初始化它及其 View 模型,即使它没有被渲染。然而,在我的示例中,viewmodel 绑定(bind)回调仅在我实际呈现 View 时执行。

另外,请记住绑定(bind)是有计划的,不会立即触发。

如果您还没有看到,这里是 Ext JS's two-way binding example .

我希望这能澄清事情!

关于javascript - 双向绑定(bind)不更新 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38665263/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com