延迟加载在ExtJS中有很多关联

时间:2013-04-17 15:14:46

标签: javascript extjs extjs4

我正在尝试使用此代码在ExtJS 4中实现延迟加载:http://dev.sencha.com/deploy/ext-4.0.0/examples/data/associations.html使用此代码:

/* Models */
Ext.define('Post', { 
    extend: 'Ext.data.Model', 
    fields: ['title'], 
    proxy: {
        type: 'ajax', 
        url: 'http://example.com/post.json'
    }
});

Ext.define('User', { 
    extend: 'Ext.data.Model', 
    fields: ['name'], 
    hasMany: 'Post', 
    proxy: { 
    type: 'ajax', 
        url : 'http://example.com/user.json'
    } 
});

/* Load User and Posts */
User.load(1, {
    success: function(user) {
        var test = user.posts();
        test.on('load', function(me) {
            console.log(me.getCount()); /* THIS is always 0?! */
        });
        test.load();
    }
});

/* Returned JSON Data */

/* User */
[{"name":"blalala"}]

/* Posts */
[{"title":"dfgdfgdgd"}]

但返回的Posts-Store始终为空(0记录)。你可以在这里查看我的JSFiddle:http://jsfiddle.net/lenoxDoe/n6Xbw/2/

任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:2)

<击> 1。 hasMany属性是一个数组

  1. 您需要在hasMany关系上设置foreignKey,并在Post
  2. 上提供外键字段
相关问题