ExtJS模型关联 [英] ExtJS models association

查看:119
本文介绍了ExtJS模型关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi StackOverflow用户!



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



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



主要型号:

  Ext.define('Invoice',{
extends:'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',键入:'string',映射:'data_source.data_source_name'}
],
关联:[
{model:'Contractor',name:'contractor' ,
{model:'SalesRepresentative',name:'salesRepresentative',type:'haveOne',associationKey:'salesRepresentative'},
{model:'InvoiceItem',name :'invoiceItems',键入:'hasMany'}
]

}) p>

两个工作:

  Ext.define('InvoiceItem',{
extends:'Ext.data.Model',
belongsTo:'Invoice',
fel ds:[
{name:'id',type:'int'},
{name:'invoice_id',type:'string'},
{name:'item_variation_id'类型:'string'},
{name:'item_quantity',type:'number'},
{name:'item_name',type:'string'},
{name: 'item_price',键入:'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',键入:'number'}
]

});

  Ext.define ('Contractor',{
extends:'Ext.data.Model',
fields:[
{name:'id',type:'int'},
{名称:'contractor_id',ty pe:'string'},
{name:'contractor_email',type:'string'},
{name:'contractor_status',type:'string'},
{name: 'contractor_registered',键入:'date',dateFormat:'time'},
{name:'contractor_name',type:'string'},
{name:'contractor_company',type:'string '},
{name:'contractor_company_vat_no',键入:'string'},
{name:'contractor_phone',type:'string'},
{name:'contractor_address'类型:'string'},
{name:'contractor_city',type:'string'},
{name:'contractor_postcode',type:'string'},
{name: 'contractor_country',键入:'string'}
]
});

第三个 - SalesRepresentative:

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



/ p>

当我使用InvoiceDetails访问承包商或存储时,任何工作都很好。但是当我尝试通过以下方式访问SalesRepresentative:

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

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



我很确定/ />

解决方案

您的商店可能会使用AJAX代理(或衍生自 Ext。 data.proxy.Server ),并尝试通过服务器请求加载销售代表记录,但找不到模型上的URL。



尝试添加

  proxy:{
type:'memory'
}

到您的模型。


Hi StackOverflow users!

I've got some json data, which should be loaded into a model with three assoctiations. Two of them are okey, but third one would not work.

Here's a json reply from server: http://pastebin.com/geL16BvL

Main Model:

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'}
]

});

Two working:

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'}
    ]
});

And Third one - 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'}
]

});

When I access Contractor or store with InvoiceDetails anything works really well. But when I try to access SalesRepresentative for example via:

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

I'm receiving "TypeError: url is return url + (url.indexOf('?') === -1 ? '?' : '&') + string;"

I'm pretty sure that somethings wrong with relations, but I can't find way to make it work.

解决方案

Your store probably uses an AJAX proxy (or any other proxy derived from Ext.data.proxy.Server) and tries to load the sales representative record via a server request, but fails to find an URL on the model.

Try adding

proxy: {
    type: 'memory'
}

to your model.

这篇关于ExtJS模型关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆