简体   繁体   English

为什么我简单的带有关联的ExtJS数据示例不起作用?

[英]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. 从我的角度来看,我在ExtJS中使用关联建立了最简单的模型方法。

Model: Post -- hasOne --> User 型号: Post - hasOne- > User

What I did: 我做了什么:

  • Using a memory proxy 使用内存代理
  • Follow the rule: Proxy in Model 请遵循以下规则:模型中的代理
  • Load a post object by Post.load(...) . 通过Post.load(...)加载post对象。

But when I try to get the user object, it is not right loaded: 但是当我尝试获取用户对象时,它没有正确加载:
(Here the full source: http://jsfiddle.net/7XRw4/4/ ) (这里是完整的来源: 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? .getUser不是异步的吗? 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. 但是我在Ext中的hasOne关系中遇到了很多问题。 Documentation, tutorials, comments on documentation - they never agree and nothing works if you don't send the complete relation in the json. 文档,教程,文档注释-如果您未在json中发送完整的关系,它们将永远无法达成共识,并且没有任何用处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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