【问题标题】:Connect two models by ID通过 ID 连接两个模型
【发布时间】:2014-04-20 13:54:03
【问题描述】:

几天前我问了一个 ExtJS 问题,作为旁注,我还问了如何连接我的两个模型。主要答案得到了回答,但我仍然无法弄清楚我的其他问题,所以我正在为此打开一个新问题。

这可能又是一个愚蠢的问题,但它是:

我从服务器得到一个 JSON,如下所示:

{
"success": true,
"result": {
    "publishers": [
        {
          "id": "009999",
          "type": "ABC",
          "isReceipient": false,
          "description": "XYZ"
        },
        {
          "id": 45,
          "type": "ABC",
          "isReceipient": true,
          "description": "XYZ"
        },
        {
          "id": 45,
          "type": "ABC",
          "isReceipient": false,
          "description": ""
        }
        ],
        "notes": [
        {
          "publisherId": "009999",
          "text": "asdasd",
          "created": "2014-02-23T18:24:06.074Z"
        },
        {
          "publisherId": "46",
          "text": "asdasd",
          "created": "2014-02-23T18:24:06.074Z"
        },
        {
          "publisherId": 45,
          "text": "asdasd",
          "created": "2014-02-23T18:24:06.074Z"
        }
    ]
}
}

所以我得到了两个数组,publishers 和 notes。我有两个模型,我通过控制器使用 loadRawData() 将它们加载到模型中,它可以工作,我在商店里有我所有的出版商和笔记。 (他们都有一个商店 - Publishers 和 Notes)。但是我需要在注释中使用 publisherId 来显示发布者的描述。我尝试了很多可以使用 google 和 sancha docs 找到的东西:associations、hasmany、hasone、belongs 并创建了由两个聚合模型组成的第三个商店。到目前为止没有任何效果。

我想要的是拥有一个包含所有笔记的商店,而且所有笔记都有发布者信息。

我将在下面复制我的两个模型,您可以在那里看到,注释掉我一直在尝试的内容。我还尝试更改 ID、名称等,因此这些变体。但我永远无法让 Notes 获得出版商的信息。

Ext.define('MA.model.Note', {
    extend: 'Ext.data.Model',
    fields: [
        'publisherId',
        'text' ,
        //hasone publisher
        {
            name: 'created',
            type: 'date',
            dateFormat: 'c'//'d-M-Y H:i:s' //"2014-02-23T18:24:06.074Z" needed: "02/23 18:24"
        }
    ]
    // hasOne: [
    //     {
    //         name: 'publisher',
    //         model: 'Publisher',
    //         associationKey: 'publisherId'
    //     }
    // ],

    // associations: [
    //     {
    //         type: 'hasOne',
    //         model: 'Publisher',
    //         primaryKey: 'id',
    //         foreignKey: 'publisherId'
    //     }
    // ]

    // associations : [
    //     {
    //         type           : 'hasOne',
    //         model          : 'MA.model.Publisher',
    //         getterName     : 'getPublisher',
    //         associatedName : 'User',
    //         associationKey : 'User'
    //     },
    //     {
    //         type           : 'belongsTo',
    //         model          : 'MA.model.Publisher',
    //         getterName     : 'getPublisher',
    //         associatedName : 'Publisher',
    //         associationKey : 'publisherId'
    //     }
    // ]

    // belongsTo: [
    //     {
    //         model: 'MA.model.Publisher',
    //         name: 'Note',
    //         primaryKey: 'publisherId',
    //         foreignKey: 'id',
    //         // foreignStore: 'Publishers'
    //     }
    // ]
});

出版商:

Ext.define('MA.model.Publisher', {
    extend: 'Ext.data.Model',

    idProperty: 'id',
    fields: [
        'id',
        'type' ,
        {
            name:'isReceipient',
            type:'boolean'
        },
        'description'
    ]
    // hasMany:  [
    //     {
    //         model: 'MA.model.Note',
    //         name: 'Note',
    //         primaryKey: 'id',
    //         foreignKey: 'publisherId',
    //         // foreignStore: 'Notes'
    //     }
    // ],
});

我是否走在正确的轨道上?我应该使用关联吗?我什至无法真正了解关联和 hasMan/One/belongTo 属性之间的区别,我想实际上没有任何区别,就像您声明它的方式一样。

编辑:我的想法是有一个 DataView 类,它有一个存储笔记和对应的发布者的笔记。我有一个主面板:

    items: [
        {
            xtype: 'create-note-panel',
            flex: 1
        },
        {
            xtype: 'notes-panel',
            store: 'Notes',
            flex: 1
        }
    ]

我的笔记面板看起来像这样:

Ext.define('MA.view.sections.notes.NotesPanel' ,{
    extend: 'Ext.DataView',
    alias: 'widget.notes-panel',
    // deferInitialRefresh: true,

    itemSelector: 'div.notes-list',
    tpl:  new Ext.XTemplate(
        '<div class="notes-list">',
            '<tpl for=".">',
                '<p>{created}, by {publisherId}</p>',
                '<p>{text}</p>',
                '<hr />',
            '</tpl>',
        '</div>'
    ),

    emptyText: 'No data available',
    initComponent: function() {
        var me = this,
            publisherStore = Ext.data.StoreManager.lookup('Publishers');
        //me.addEvents(   //just messing here, trying stuff
        //    'user-offer-activities'
        //);
        me.callParent(arguments);
    }
    //renderTo: Ext.getBody()

})
;

注意模板中的 publisherId。我需要那里的出版商描述。我不想使用网格,因为这个 DataView 似乎是一个很好的解决方案,我认为加入两个商店很容易,我只是想不通:(

【问题讨论】:

  • 你想在哪里使用这个模型的?在网格或表格中!我想根据您的要求创建一个小提琴。
  • 我希望在 DataView 中使用它,它有一个商店,并且该商店应该有注释和相应的发布者描述。至少这是我的想法,主要目标是显示所有按发布日期排序的注释以及出版商描述。我在这里没有使用任何网格。我在 DataView 中制作了一个模板,该模板当前连接到 Notes 存储,但我缺少发布者的描述。 p.s.:我会附上 DataView 类,我会启动我的笔记本。
  • 我认为您使用关联的方法是正确的。
  • 澄清一下:hasMany / hasOne 和belongTo 是3 种不同的关联。请参阅docs.sencha.com/extjs/4.2.3/#!/api/… 的文档(以及“进一步阅读”部分的链接)。您应该尝试: - 使用关联的 foreignKey 和 primaryKey 属性通过它们的 ID 连接模型 - 或者可能更改您的 json 以便模型的数据嵌套
  • 您可以将您的工作代码提供给http://fiddle.sencha.com/ 吗?以便我们为您提供更多帮助。

标签: extjs


【解决方案1】:

我创建了一个小提琴,它可以产生你所追求的结果(在视图中显示来自两个模型的数据)。

虽然这有点冗长,但由于 tpl 的工作方式,您无法访问模型,只能访问其中的数据。所以我在 tpl 上创建了一个函数,根据 publisherId 从模型中获取我们感兴趣的记录。

https://fiddle.sencha.com/#fiddle/o9o

注意:我在没有使用模型之间的任何关联的情况下创建了小提琴,但您可能会创建一个从 Notes 到 Publisher 的 hasOne 关联,并使用一个 foreignKey 链接各个模型中的 id 和 publisherId(尽管我仍然不认为这将使您能够直接在 tpl 中引用成员)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-04
    • 2012-06-11
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    相关资源
    最近更新 更多