简体   繁体   中英

Why my simple ExtJS data example with associations does not work?

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?
        }
    });

});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM