【问题标题】:Why my simple ExtJS data example with associations does not work?为什么我的带有关联的简单 ExtJS 数据示例不起作用?
【发布时间】:2013-02-01 15:13:01
【问题描述】:

从我的角度来看,我在 ExtJS 中使用关联构建了最简单的模型。

型号: 发布 --hasOne--> 用户

我做了什么:

  • 使用内存代理
  • 遵循规则:模型中的代理
  • 通过Post.load(...) 加载帖子对象。

但是当我尝试获取用户对象时,它没有正确加载:
(此处为完整来源: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?
        }
    });

});

【问题讨论】:

    标签: javascript extjs proxy associations has-one


    【解决方案1】:

    .getUser 不是异步的吗?这意味着您应该为其提供一个成功函数并在该成功函数中提醒用户名?

    但我在 Ext 中的 hasOne 关系方面遇到了很多问题。文档、教程、文档中的 cmets - 如果您不在 json 中发送完整的关系,他们永远不会同意,并且没有任何效果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 2017-11-25
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      相关资源
      最近更新 更多