Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

ExtJS models association

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...