Sencha Touch商店协会不匹配?

时间:2014-05-09 08:19:15

标签: javascript extjs sencha-touch

我有两个带关联的模型(Category - > Object)

Category
    |--Has many--->Objects

以下是 CategoryModel

Ext.define("TouchApp.model.CategoryModel", {
    extend: "Ext.data.Model",
    config: {
        fields: [
            {
                name: 'id',
                type: 'int'
            },
            {
                name: 'name',
                type: 'String'
            }
        ],
        //Aide: http://docs.sencha.com/touch/2.2.1/#!/api/Ext.data.association.Association
        associations: [
            { type: 'hasMany', model: 'ObjectsModel', primaryKey: 'id', foreignKey: 'category'}
        ]

    }
});

这是 ObjectsModel

Ext.define("TouchApp.model.ObjectsModel", {
    extend: "Ext.data.Model",
    config: {
        fields: [
            {
                name: 'id',
                type: 'int'
            },
            {
                name: 'name',
                type: 'String'
            },
            {
                name: 'category',
                type: 'int'
            }
        ],
        //Aide: http://docs.sencha.com/touch/2.2.1/#!/api/Ext.data.association.Association
        associations: [
            { type: 'belongsTo', model: 'CategoryModel', primaryKey: 'id', foreignKey: 'category'}
        ]
    }
});

这种关联对吗?否则,也许我不需要在两边设置它? 我收到了警告[WARN][Ext.data.Operation#process] Unable to match the updated record that came back from the server.

PS:我需要这些模型将相关的商店放在一个嵌套列表中。

1 个答案:

答案 0 :(得分:0)

我终于找到了一个寻找at this exemple!

的解决方案

这是使用每个关联案例的一个非常好的例子。

我终于理解了这个概念。

我们需要在模型中设置idProperty并使用hasMany和belongsTo关联。

<强> CategoryModel:

Ext.define("TouchApp.model.CategoryModel", {
    extend: "Ext.data.Model",
    config: {
        idProperty: 'id', //The idProperty representing the "PK"
        fields: [
            {
                name: 'id',
                type: 'int'
            },
            {
                name: 'name',
                type: 'String'
            }
        ],
        //Really great example: http://docs.sencha.com/touch/2.2.1/#!/api/Ext.data.association.Association
        hasMany: [{ model: 'TouchApp.model.ObjectsModel' }] //The hasMany association setting the model associated...
    }
});

<强> ObjectsModel:

Ext.define("TouchApp.model.ObjectsModel", {
    extend: "Ext.data.Model",
    config: {
        idProperty: 'id', //The idProperty representing the "PK"
        fields: [
            {
                name: 'id',
                type: 'int'
            },
            {
                name: 'name',
                type: 'String'
            },
            {
                name: 'category',
                type: 'int'
            }
        ],
        //Realy great example: http://appointsolutions.com/2012/07/using-model-associations-in-sencha-touch-2-and-ext-js-4/
        belongsTo: [{ model: 'TouchApp.model.CategoryModel', associationKey: 'category' }] //Here the belongsTo association which tell the parent Model. AssociationKey set which key is the "FK". It will be linked to the idProperty
    }
});

就是这样。但是现在我的嵌套列表不起作用......