Sebastien
Sebastien

Reputation: 6660

How to get a hasOne model with sencha touch

I have a model Banner and a model BannerFormat. A banner has a banner format. To configure a hasOne relationship between banner and banner format, i wrote this :

Ext.define('admin.model.Banner', {
    extend: 'Ext.data.Model',
    config: {
        fields: [
            { name: 'id', type: 'int' },
            { name: 'banner_format_id', type: 'int' },
            'code',
            'active',
            'start_at',
            'end_at'
        ],
        associations: { type: 'hasOne', model: 'admin.model.BannerFormat', getterName: 'getBannerFormat' },
        proxy: {
            type: 'ajax',
            url: '/admin/api_query.php',
            extraParams: {
                table: 'content_banners',
                type: 'GET'
            }
        }
    }
});

And in my bannerFormat model:

Ext.define('admin.model.BannerFormat', {
    extend: 'Ext.data.Model',
    config: {
        fields: ['id', 'format'],
        associations: { type: 'hasMany', model: 'admin.model.Banner' },
        proxy: {
            type: 'ajax',
            url: '/admin/api_query.php',
            extraParams: {
                table: 'content_banner_formats',
                type: 'GET'
            }
        }
    }
});

But when i call banner.getBannerFormat(), i got :

Uncaught TypeError: Object [object Object] has no method 'getBannerFormat'

What did i go wrong?

Upvotes: 1

Views: 1348

Answers (1)

Eric C
Eric C

Reputation: 832

Are you creating a banner object and then making the call on it to retrieve the defined associations?

This should work for you:

var banner = Ext.create('admin.model.Banner', {
    id: 100,
    banner_format_id: 20,
    code: 'ABC123',
    active: true,
    start_at: 1,
    end_at: 5
});

banner.getBannerFormat();

Also, you don't need to specify a getterName for the association if you don't want. Sencha will auto-generate a getter function for the association, which follows the format: 'getModelName'. If you removed the getterName from your association, the getter function on the banner model would be the same as what you defined it as: 'getBannerFormat';

Upvotes: 1

Related Questions