2012-12-23 42 views
0

我想在datalist上获取belongsTo记录并显示父记录字段。Sencha Touch belongsTo关联使用Store on Datalist

Ext.define('MyApp.model.Customer', { 
    extend: 'Ext.data.Model', 

    config: { 
     fields: ['Id', 
     'EMail'], 
     hasMany: [{ 
      model: 'MyApp.model.OutstandingInvoice', 
      name: 'OutstandingInvoice', 
      primaryKey: 'Id', 
      foreignKey: 'customerId', 
      foreignStore: 'OutstandingInvoices' 
     }] 
    } 
}); 

Ext.define('MyApp.model.OutstandingInvoice', { 
    extend: 'Ext.data.Model', 

    config: { 
     fields: [ 
     'InvoiceDate', 
     'InvoiceID', 
     'customerId' 
     ], 
     belongsTo: [{ 
      model: 'MyApp.model.Customer', 
      name: 'Customer', 
      primaryKey: 'Id', 
      foreignKey: 'customerId', 
      foreignStore: 'Customers' 
     }] 
    } 
}); 


Ext.define('MyApp.store.OutstandingInvoices', { 
    extend: 'Ext.data.Store', 

    config: { 
     model: 'MyApp.model.OutstandingInvoice',   
     storeId: 'OutstandingInvoiceStore',   
     proxy: { 
      useDefaultXhrHeader: false, 
      type: 'ajax',    
      url : 'http://localhost/getOutstandingInvoices',    
      reader: { 
       type: 'json' 
      } 
     }, 
     autoLoad: false 
    } 
}); 


Ext.define('MyApp.store.Customers', { 
    extend: 'Ext.data.Store', 

    config: { 
     model: 'MyApp.model.Customer',  
     storeId: 'CustomerStore',   
     proxy: { 
      useDefaultXhrHeader: false, 
      type: 'ajax',    
      url : 'http://localhost/getCustJList', 
      reader: { 
       type: 'json' 
      } 
     }, 
     autoLoad: false, 
     sorters: [{ 
      property : 'FName', 
      direction: 'ASC' 
     }] 
    } 
}); 



Ext.define('MyApp.view.OutstandingInvoices', { 
    extend: 'Ext.Panel', 
    xtype: 'outstandingInvoicesXType', 
    config: { 
     cls : 'invoiceSummaryCls', 
     scrollable: 'vertical', 
     items: [ 
     { 
      xtype: 'titlebar', 
      docked: 'top', 
      title: 'Outstanding Invoices' 
     }, 
     { 
      xtype : 'list', 
      scrollable: false, 
      store: 'OutstandingInvoiceStore', 
      cls : 'p10', 
      itemTpl: [ 
      '<div>Invoice # {InvoiceID}</div>', 
      '<div>{InvoiceDate}</div>',   
      '<div>{Customer.Email}</div>', // I want to show Customer name here as its belongsTo Customer 
      ], 
      listeners: { 
       itemtap:function (list, index, targe, rec, e, eOpts) {      
        console.log(rec) 
       } 
      } 
     } 
     ] 
    } 

}); 

我想表明在数据列表客户名称,但有问题的公司章程或Xtemplate 我收到此错误

Uncaught Error: [ERROR][Ext.XTemplate#apply] Cannot read property 'Email' of undefined 

请帮我在这。

+0

在客户存储区中尝试autoLoad:true –

+0

这不会帮助确定该错误但仅供参考,您的客户字段配置中存在拼写错误:EMail而不是电子邮件 –

+0

您需要将代理移动到模型上,否则sencha将不知道如何为关联加载数据 –

回答

1

我推荐读这个article,它很长,但最终的List部分与您的示例类似。

我认为重点是你不需要单独的商店。 Sencha将会在关联背后自动创建这些内容。确保将代理移动到模型上并设置autoLoad:true