ExtJS模型关联

时间:2013-10-14 12:22:34

标签: extjs models

您好StackOverflow用户!

我有一些json数据,应该加载到一个有三个关联的模型中。其中两个是okey,但第三个不起作用。

这是服务器的json回复: http://pastebin.com/geL16BvL

主要型号:

Ext.define('Invoice', {
extend: 'Ext.data.Model',
fields: [
    {name: 'id', type: 'int'},
    {name: 'invoice_id', type: 'string'},
    {name: 'invoice_date', type: 'date', dateFormat: 'time'},
    {name: 'invoice_due_date', type: 'date', dateFormat: 'time'},
    {name: 'invoice_year_index', type: 'int'},
    {name: 'invoice_total', type: 'number'},
    {name: 'invoice_vat', type: 'number'},
    {name: 'invoice_delivery', type: 'number'},
    {name: 'invoice_total_cost', type: 'number'},
    {name: 'invoice_payment_method', type: 'string'},
    {name: 'invoice_status', type: 'string'},
    {name: 'invoice_discount', type: 'number'},
    {name: 'invoice_discount_type', type: 'string'},
    {name: 'invoice_fixed_charges', type: 'number'},
    {name: 'invoice_currency_code', type: 'string'},
    {name: 'invoice_currency_symbol', type: 'string'},
    {name: 'localization_data_source', type: 'string', mapping: 'data_source.data_source_name'}
],
associations: [
    {model: 'Contractor', name: 'contractor', type: 'hasOne'},
    {model: 'SalesRepresentative', name: 'salesRepresentative', type: 'hasOne', associationKey: 'salesRepresentative'},
    {model: 'InvoiceItem', name: 'invoiceItems', type: 'hasMany'}
]

});

两个工作:

Ext.define('InvoiceItem', {
extend: 'Ext.data.Model',
belongsTo: 'Invoice',
fields: [
    {name: 'id', type: 'int'},
    {name: 'invoice_id', type: 'string'},
    {name: 'item_variation_id', type: 'string'},
    {name: 'item_quantity', type: 'number'},
    {name: 'item_name', type: 'string'},
    {name: 'item_price', type: 'number'},
    {name: 'item_value', type: 'number'},
    {name: 'item_position_vat', type: 'number'},
    {name: 'item_vat', type: 'number'},
    {name: 'item_tax', type: 'number'},
    {name: 'item_category_name', type: 'string'},
    {name: 'item_discount', type: 'number'},
    {name: 'item_price_before_discount', type: 'number'}
]

});

Ext.define('Contractor', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'id', type: 'int'},
        {name: 'contractor_id', type: 'string'},
        {name: 'contractor_email', type: 'string'},
        {name: 'contractor_status', type: 'string'},
        {name: 'contractor_registered', type: 'date', dateFormat: 'time'},
        {name: 'contractor_name', type: 'string'},
        {name: 'contractor_company', type: 'string'},
        {name: 'contractor_company_vat_no', type: 'string'},
        {name: 'contractor_phone', type: 'string'},
        {name: 'contractor_address', type: 'string'},
        {name: 'contractor_city', type: 'string'},
        {name: 'contractor_postcode', type: 'string'},
        {name: 'contractor_country', type: 'string'}
    ]
});

第三个 - SalesRepresentative:

Ext.define('SalesRepresentative', {
extend: 'Ext.data.Model',
belongsTo: 'Invoice',
fields: [
    {name: 'id', type: 'int'},
    {name: 'representative_id', type: 'string'},
    {name: 'representative_name', type: 'string'},
    {name: 'representative_email', type: 'string'}
]

});

当我使用InvoiceDetails访问Contractor或商店时,任何效果都非常好。但是当我尝试访问SalesRepresentative时,例如通过:

Ext.getStore('invoicesStore').getAt(0).getSalesRepresentative()

我收到了 “TypeError:url是 return url +(url.indexOf('?')=== -1?''':'&')+ string;“

我很确定关系中的某些事情是错误的,但我找不到让它发挥作用的方法。

1 个答案:

答案 0 :(得分:2)

您的商店可能使用AJAX代理(或从Ext.data.proxy.Server派生的任何其他代理)并尝试通过服务器请求加载销售代表记录,但无法在模型上找到URL。

尝试添加

proxy: {
    type: 'memory'
}

到你的模特。