ExtJS4:仅第一个嵌套属性具有多个加载的模型

ExtJS4:仅第一个嵌套属性具有多个加载的模型,extjs,sencha-touch,extjs4,sencha-touch-2,Extjs,Sencha Touch,Extjs4,Sencha Touch 2,我试图通过模型的load函数从具有嵌套属性的服务器加载JSON回复。但不知怎的,只有嵌套属性集的第一个条目被解析 我正在尝试加载一个JSON文件,如下所示: { "id" : "2", "name" : "John", "logins" : [ { "id" : "4", "person_id" : "2", "date" : "2012-01-18 01:00:06" },{ "i

我试图通过模型的load函数从具有嵌套属性的服务器加载JSON回复。但不知怎的,只有嵌套属性集的第一个条目被解析

我正在尝试加载一个JSON文件,如下所示:

{
   "id" : "2",
   "name" : "John",
   "logins" : [
      {
         "id" : "4",
         "person_id" : "2",
         "date" : "2012-01-18 01:00:06"
      },{
         "id" : "9",
         "person_id" : "2",
         "date" : "2012-01-18 19:36:13"
      },{
         "id" : "12",
         "person_id" : "2",
         "date" : "2012-01-19 00:12:32"
      }]
}
Ext.define('MyAppName.model.Person', {
    extend : 'Ext.data.Model',
    config : {
        idProperty : 'id',
        fields : [{
                    name : 'id',
                    type : 'int'
                }, {
                    name : 'name',
                    type : 'string'
                }],
        hasMany : [{
                model: 'MyAppName.model.Login',
                name: 'logins',
                associationKey: 'logins'
        }],
        proxy : {
            type : 'ajax',
            url : '../index.php?format=json',
            reader : {
                type : 'json'
            }
        }
    }
});
我有两个模型,如下所示:

{
   "id" : "2",
   "name" : "John",
   "logins" : [
      {
         "id" : "4",
         "person_id" : "2",
         "date" : "2012-01-18 01:00:06"
      },{
         "id" : "9",
         "person_id" : "2",
         "date" : "2012-01-18 19:36:13"
      },{
         "id" : "12",
         "person_id" : "2",
         "date" : "2012-01-19 00:12:32"
      }]
}
Ext.define('MyAppName.model.Person', {
    extend : 'Ext.data.Model',
    config : {
        idProperty : 'id',
        fields : [{
                    name : 'id',
                    type : 'int'
                }, {
                    name : 'name',
                    type : 'string'
                }],
        hasMany : [{
                model: 'MyAppName.model.Login',
                name: 'logins',
                associationKey: 'logins'
        }],
        proxy : {
            type : 'ajax',
            url : '../index.php?format=json',
            reader : {
                type : 'json'
            }
        }
    }
});

我试着通过

MyAppName.model.Person.load(personId, {scope: ..., ..., success: function(record, operation) { -someFancyCode-} })
但是我在success函数中检索的记录现在只包含一个登录数据集。我做错什么了吗

谢谢!
Nic

首先,模型本身不加载嵌套数据,至少不是很好,它在这方面有一些已知的问题,因此,更好的选择是创建一个存储并通过该存储加载模型,这些行中的某些行:

Ext.define('MyAppName.model.Person', {
    extend: 'Ext.data.Model',
    idProperty: 'id',
    fields: [{
        name: 'id',
        type: 'int'
    }, {
        name: 'name',
        type: 'string'
    }],
    hasMany: [{
        model: 'MyAppName.model.Login',
        name: 'logins'
    }],
    proxy: {
        type: 'memory',
        reader: {
            type: 'json'
        }
    }
});

Ext.define('MyAppName.model.Login', {
    extend: 'Ext.data.Model',
    fields: [{
        name: 'id',
        type: 'int'
    }, {
        name: 'person_id',
        type: 'int'
    }, {
        name: 'date',
        type: 'date'
    }],
    belongsTo: [{
        model: 'MyAppName.model.Person'
    }]
});
var data = {
    "id": "2",
    "name": "John",
    "logins": [{
        "id": "4",
        "person_id": "2",
        "date": "2012-01-18 01:00:06"
    }, {
        "id": "9",
        "person_id": "2",
        "date": "2012-01-18 19:36:13"
    }, {
        "id": "12",
        "person_id": "2",
        "date": "2012-01-19 00:12:32"
    }]
};
var store = Ext.create('Ext.data.Store', {
    model: 'MyAppName.model.Person',
    data: data
});
var instanceModel = store.getAt(0);
//Show 'Person' model contents.
console.info(instanceModel);

//Show 'logins' for that person
console.info(instanceModel.logins());
检查相应的


希望这能让您走上正确的轨道。

首先,模型本身不会加载嵌套数据,至少不是很好,它在这方面存在一些已知的问题,因此,更好的选择是创建一个存储并通过该存储加载模型,这些行包括:

Ext.define('MyAppName.model.Person', {
    extend: 'Ext.data.Model',
    idProperty: 'id',
    fields: [{
        name: 'id',
        type: 'int'
    }, {
        name: 'name',
        type: 'string'
    }],
    hasMany: [{
        model: 'MyAppName.model.Login',
        name: 'logins'
    }],
    proxy: {
        type: 'memory',
        reader: {
            type: 'json'
        }
    }
});

Ext.define('MyAppName.model.Login', {
    extend: 'Ext.data.Model',
    fields: [{
        name: 'id',
        type: 'int'
    }, {
        name: 'person_id',
        type: 'int'
    }, {
        name: 'date',
        type: 'date'
    }],
    belongsTo: [{
        model: 'MyAppName.model.Person'
    }]
});
var data = {
    "id": "2",
    "name": "John",
    "logins": [{
        "id": "4",
        "person_id": "2",
        "date": "2012-01-18 01:00:06"
    }, {
        "id": "9",
        "person_id": "2",
        "date": "2012-01-18 19:36:13"
    }, {
        "id": "12",
        "person_id": "2",
        "date": "2012-01-19 00:12:32"
    }]
};
var store = Ext.create('Ext.data.Store', {
    model: 'MyAppName.model.Person',
    data: data
});
var instanceModel = store.getAt(0);
//Show 'Person' model contents.
console.info(instanceModel);

//Show 'logins' for that person
console.info(instanceModel.logins());
检查相应的

希望这能让你走上正轨