Yurii Buhryn
Yurii Buhryn

Reputation: 823

Sencha Touch 2 association problems

I get from server next JSON:

{
    "contextPath":"http://apps.dhis2.org/demo",
    "user":{
        "id":"GOLswS44mh8",
        "name":"System Administrator",
        "isAdmin":true,
        "ou":{
             "id":"ImspTQPwCqd",
             "name":"Sierra Leone"
        }
     }
}

I need convert this JSON in two Models : User model and OrganizationUnit model.

I read this tutorial http://docs.sencha.com/touch/2.2.1/#!/api/Ext.data.reader.Reader so it my code :

User model :

Ext.define('mobile-visualizer.model.User', {
    extend: 'Ext.data.Model',

    config: {
        fields: [
            {name: 'id',  type: 'string'},
            {name: 'name',   type: 'string'},
            {name: 'isAdmin', type: 'boolean'}
        ],
        hasOne: {
            model: "mobile-visualizer.model.OrganizationUnit",
            name: "ou"
        },
        proxy: {
            type: 'ajax',
            url : 'http://apps.dhis2.org/demo/dhis-web-visualizer/initialize.action',
            method : 'GET',
            withCredentials : true,
            useDefaultXhrHeader : false,
            reader: {
                type: 'json',
                rootProperty: 'user'
            }
        }
    }
});

OrganizationUnit model :

Ext.define('mobile-visualizer.model.OrganizationUnit', {
    extend: 'Ext.data.Model',

    config: {
        fields: [
            {name: 'id',  type: 'string'},
            {name: 'name',   type: 'string'}
        ],
        belongsTo: 'mobile-visualizer.model.User'
    }
});

Store :

Ext.define('mobile-visualizer.store.UsersStore', {
    extend : 'Ext.data.Store',
    model : "mobile-visualizer.model.User",
    autoLoad : false,
    storeId : "usersStore",

    proxy : {
        type : 'ajax',
        url : 'http://apps.dhis2.org/demo/dhis-web-visualizer/initialize.action',
        method : 'GET',
        withCredentials : true,
        useDefaultXhrHeader : false,
        reader : {
            type : 'json',
            rootProperty : 'user'
        }
    }
});

So, when I try get user from store using another code :

        var store = Ext.create('mobile-visualizer.store.UsersStore');
        store.load({
                    callback : function() {
                        // the user that was loaded
                        var user = store.first();
                        console.log(user.ou())
                    }
                });

I have error : Uncaught TypeError: Object [object Object] has no method 'ou'

I can get all information about user but I can't get ou from user. It look like some association issue.

But I make all as like official tutorial . Please help to resolve this problem. Thanks.

Upvotes: 1

Views: 594

Answers (1)

Neil McGuigan
Neil McGuigan

Reputation: 48267

Follow my rules and everything will come out rosy:

http://extjs-tutorials.blogspot.ca/2012/05/extjs-hasmany-relationships-rules.html

http://extjs-tutorials.blogspot.ca/2012/05/extjs-belongsto-association-rules.html

These are for extjs, but very similar for sencha touch.

You are forgetting the associationKey.

Also, no need to redefine proxy in store as it will inherit its model's proxy.

Also, a user probably belongs to an org unit and doesn't have one...

Upvotes: 1

Related Questions