0

I'm having trouble trying to invoke getters/setters on a Model object that has an association with one other model. Here are the classes:

Category.js

Ext.define('Chapter05.model.Category', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'id',   type: 'int' },
        { name: 'name', type: 'string' }
    ]
})

Product.js

Ext.define('Chapter05.model.Product', {
    extend: 'Ext.data.Model',

    requires: [
        'Chapter05.model.Category'
    ],

    fields: [
        { name: 'id',          type: 'int' },
        { name: 'category_id', type: 'int' },
        { name: 'name',        type: 'string' }
    ],
    // we can use the belongsTo shortcut on the model to create a belongsTo association
    associations: [
        { type: 'belongsTo', model: 'Chapter05.model.Category' }
    ]
})

Main.js

Ext.define('Chapter05.view.Main', {
    extend: 'Ext.container.Container',
    requires:[
        'Ext.tab.Panel'
        'Chapter05.model.Product',
        'Chapter05.model.Category',
    ],

    xtype: 'app-main',
    layout: 'vbox',

    items: [
        {
            xtype: 'button',
            text: 'Category',

            handler: function(evt) {
                var product = new Chapter05.model.Product({
                    id: 100,
                    category_id: 20,
                    name: 'Sneakers'
                });

                product.getCategory(function(category, operation) {
                    // do something with the category object
                    alert(category.get('id')); // alerts 20
                }, this);
            }
        }
    ]
});

The error occurs at the line where product.getCategory(...) is. I get the following message in Safari Web Inspector:

TypeError: 'undefined' is not a function (evaluating 'product.getCategory')

Am I forgetting to do something?

P.S. The project(Chapter05) was generated using Sencha Cmd. Hence, the fully qualified names.

Android Noob
  • 3,271
  • 4
  • 34
  • 60

1 Answers1

1

I've had a similar problem with a hasOne relation. It was solved by specifying the getters/setters and the associationKey yourself.

Something like:

belongsTo: {
    model: 'Chapter05.model.Category',
    getterName: 'getCategory',
    setterName: 'setCategory',
    associationKey: 'category_id'
}
Carl
  • 740
  • 8
  • 18
  • I added all the stuff from your post, but now I got this error instead: Ext.data.proxy.Server.buildUrl(): You are using a ServerProxy but have not supplied it with a url. Did a quick search on Google and tried adding idProperty field to both of the Models as well. Still got the same error. Any ideas? – Android Noob Dec 30 '13 at 17:35