0

from my point of view I build the simplest way of model with associations in ExtJS.

Model: Post --hasOne--> User

What I did:

  • Using a memory proxy
  • Follow the rule: Proxy in Model
  • Load a post object by Post.load(...).

But when I try to get the user object, it is not right loaded:
(Here the full source: http://jsfiddle.net/7XRw4/4/)

Ext.define('Post', {
    extend: 'Ext.data.Model',
    fields: ['user_id', 'content'],
    hasOne: 'User',
    proxy: {
        type: 'memory',
        data: {
            posts: [
                {id:1, user_id:1, content:'foo'},
                {id:2, user_id:1, content:'bar'}
            ]
        },
        reader: {
            type: 'json',
            root: 'posts'
        }
    }
});


Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: ['name'],
    proxy: {
        type: 'memory',
        data: {
            users: [
                {id:1, name:'hans'},
                {id:2, name:'klaus'}
            ]
        },
        reader: {
            type: 'json',
            root: 'users'
        }
    }
});

Ext.onReady(function() {
    console.log("Ext.onReady() invoked...");

    Post.load('1', {
        success: function(record, operation) {
            thePost = record;

            console.log('thePost:', thePost);
            theUser = thePost.getUser();
            console.log('theUser:', theUser);

            alert('The user name: ' + theUser.get('name'));
            // The user object will not be right loaded! Why?
        }
    });

});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Fred
  • 1
  • 2

1 Answers1

0

Isn't .getUser asynchronous? Meaning you should supply it with a success function and alert the user's name in that success function?

But I have had plenty of problems with hasOne relations in Ext. Documentation, tutorials, comments on documentation - they never agree and nothing works if you don't send the complete relation in the json.

oldwizard
  • 5,012
  • 2
  • 31
  • 32