【问题标题】:ExtJS: How do I bind a label to a ViewModel store with a Single recordExtJS:如何将标签绑定到具有单个记录的 ViewModel 存储
【发布时间】:2026-01-31 11:55:01
【问题描述】:

请看下面的小提琴:Bind store from a ViewModel to an xtype label

我无法从 ViewModel 的商店中获取记录以显示在表单的 xtype: 'label' 项中。

这应该很容易,但是我的大脑无法工作......

【问题讨论】:

    标签: extjs data-binding viewmodel


    【解决方案1】:

    无法为商店的第一条记录创建绑定描述符。为此,您需要实现formula

    在您的视图模型中:

    formulas: {
        firstTestStoreRecord: {
            bind: '{testStore}',
            get: function(testStore) {
                return testStore.getAt(0);
            }
        }
    }
    

    然后在你的视图中使用它:

    bind: {
        html: '<b>{firstTestStoreRecord.test}</b>'
    }
    

    这是工作小提琴:https://fiddle.sencha.com/#fiddle/25cf&view/editor

    【讨论】:

    • 工作就像一个魅力!
    • @scebotari66 “无法为商店的第一条记录创建绑定描述符”仅供参考,从 6.5 开始,这不再适用:fiddle.sencha.com/#fiddle/25ck&view/editor
    • @EvanTrimboli 我不知道。感谢您的提示!
    • 您可以绑定到 first/last/count/totalCount。您也可以绑定到模型dirty/phantom/valid,新添加到6.5。
    【解决方案2】:

    在 7.4 版中:

    bind: {
        html: '<b>{testStore.first.test}</b>'
    }
    

    【讨论】: