1. #1
    Sencha Premium Member
    Join Date
    Dec 2013
    Posts
    11
    Vote Rating
    0
    theSenchaNoob is on a distinguished road

      0  

    Default Having problems with getters/setters generated by Associations

    Having problems with getters/setters generated by Associations


    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
    Code:
    Ext.define('Chapter05.model.Category', {
        extend: 'Ext.data.Model',
        fields: [
            { name: 'id',   type: 'int' },
            { name: 'name', type: 'string' }
        ]
    })

    Product.js

    Code:
    Ext.define('Chapter05.model.Product', {
        extend: 'Ext.data.Model',
        
        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

    Code:
    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:


    Code:
    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.

  2. #2
    Sencha - Support Team slemmon's Avatar
    Join Date
    Mar 2009
    Location
    Boise, ID
    Posts
    6,062
    Vote Rating
    215
    slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of

      0  

    Default


    You may need to set the getterName in the belongsTo config explicitly:
    http://docs.sencha.com/extjs/4.2.2/#...cfg-getterName


    Are you a Sencha products veteran who has wondered what it might be like to work at Sencha? If so, please reach out to our recruiting manager:
    sheryl@sencha.com

Thread Participants: 1

Tags for this Thread