【问题标题】:ExtJs ViewModel doesn't display data even if it's thereExtJs ViewModel 不显示数据,即使它在那里
【发布时间】:2015-12-20 08:56:55
【问题描述】:

我有以下型号:

Ext.define('myApp.model.tool.SpecialModel', {
    extend: 'Ext.data.Model',

    fields: [
        {name: 'id', type: 'string'},
        {name: 'loginName', type: 'string'},
        {name: 'firstName', type: 'string'},
        {name: 'lastName', type: 'string'},
    ],

    proxy: {
        type: 'rest',
        url: '/special/url',
        reader: {
            type: 'json'
        }
    }

});

还有这个 ViewModel:

Ext.define('myApp.view.support.MySpecialViewModel', {
    extend: 'Ext.app.ViewModel',
    alias: 'viewmodel.myspecial',

    stores: {
        session: {
            model: 'myApp.model.tool.SpecialModel'
        }
    }

});

还有这个观点:

Ext.define('myApp.view.support.MySpecialView', {
    extend: 'Ext.toolbar.Toolbar',

    viewModel: 'myspecial',

    config: {
        items: [
            {
                xtype: 'button',
                bind: '{session.lastName}',
                menu: [
                    {
                        text: 'My Profile',
                        handler: 'onProfileClick'
                    }
                ]
            }
        ]
    },

});

在渲染视图时,商店已经被填满。 为什么按钮不显示指定 ViewModel 的 lastName? 当我尝试使用内联数据时,它可以工作。

提前致谢。

编辑:当我将按钮绑定更改为以下内容时

...

                    xtype: 'button',
                    bind: 'Hello, {session.lastName}',
                    menu: [
                        {
                            text: 'My Profile',
                            handler: 'onProfileClick'
                        }
            ]
...

甚至不显示硬编码的字符串。我在这里错过了什么?

编辑2

这是我的 json 数据:

{
    "privileges": {
        "privManageCustomer": true,
        "privManageCustomerAccount": true,
    },
    "user": {
        "accountLocked": false,
        "accountLockedAt": null,
        "customerId": "12123213213123",
        "email": "213213",
        "failedLogins": 0,
        "firstName": null,
        "id": "123678213621783",
        "languageId": "de",
        "lastFailedLogin": null,
        "lastLogin": null,
        "lastName": "Whatever",
        "loginName": "test123"
    }
}

【问题讨论】:

  • 你能把url的json样本发给我们吗:'/special/url',还有一个小提琴会更好
  • @KalaiarasanManimaran 添加了我的数据。
  • 我不太确定绑定是否可以通过这种方式完成。但无论如何,首先你错过了阅读器的 rootProperty 配置,所以阅读器不知道从什么json的一部分来获取数据。 rootProperty 应该等于“用户”。其次 - 我永远不会将我的商店或数据元素命名为“会话”,而它可以与保留的会话对象相关联。如果仍然不起作用,请尝试绑定到 {session.data.items.0.lastName}。

标签: javascript extjs mvvm extjs6


【解决方案1】:

我认为你做错了。您应该使用带有模型的商店。 请看“5. 实验 2:创建数据层”,这个小教程 (http://ladysign-apps.com/appcamp/slides/#2014-07-14_13-50_00-670_Z) 由 Sencha 的 Lee Boonstra 制作。我始终遵循她的指导方针,从未在我的商店和数据绑定方面遇到任何问题。

我还注意到您使用的是 {session.lastName},如果商店加载正确,它实际上应该是 {lastName}。

【讨论】:

  • 感谢您的链接,非常感谢!但它仍然没有工作。我稍后再试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-10
相关资源
最近更新 更多