2014-04-20 20 views
3

我问一个ExtJS的问题,前几天,作为一个方面说明我还问我怎么能连接我的两个型号。主要答案得到了回答,但我仍然无法弄清楚我的其他问题,所以我正在为此提出一个新问题。连接两种型号的ID

它可能再次成为一个愚蠢的问题,但在这里它是:

我从服务器获取一个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" 
     } 
    ] 
} 
} 

所以我得到两个数组,出版商和注意事项。我有两个模型,我使用loadRawData()将它们加载到模型中,它起作用,我在商店中获得了所有的发布者和笔记。 (他们都有一家商店 - 出版商和笔记)。但是,我需要使用notes中的publisherId来显示发布者描述。我尝试了很多东西,我能找到使用谷歌和三岔文档:协会的hasMany,hasone,属于关联和创建第三家店由两个聚合模式。目前为止没有任何工作

我想要的是有一个具有每一个音符一个店,加上所有的笔记都发布信息。

下面我将复制我的两个型号,你可以看到有评论说什么我一直在努力。我也尝试改变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/belongsTo属性之间的区别,我想没有任何真正的东西,就像你声明它的方式一样。

编辑:我的想法是有一个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中一个不错的解决方案,我没想过两家店是容易的,我只是无法弄清楚尚未:(

+1

您想在哪里使用此模型?在网格或表单中!我想根据您的要求创建一个小提琴。 –

+1

我希望在具有商店的DataView中使用它,并且该商店应该有注释和相应的发布者描述。至少这是我的想法,主要目标是显示按出版商描述发布日期排序的所有注释。我在这里没有使用任何网格。我在DataView中创建了一个模板,该模板目前已连接到Notes商店,但我缺少发布商描述。 p.s .:我会附加DataView类,我只会启动我的笔记本。 – Revolit

+0

我认为你使用关联是正确的。 –

回答

0

我创建了一个小提琴导致(在视图中显示来自两个模型的数据)

虽然tpl的工作方式有点不同,但您无法访问模型,所以我在tpl上创建了一个函数,该函数从基于publisherId的模型中获取我们感兴趣的记录。

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

注:我创建了小提琴,而无需使用模型之间的任何关联,但你可能会创建一个从Notes中的hasOne关联出版社与外键连接在各自车型的ID和两个publisherId(虽然我仍然认为这不会让你直接引用tpl中的成员)。