Sencha touch 在Sencha Touch 2中创建多个关联

Sencha touch 在Sencha Touch 2中创建多个关联,sencha-touch,has-many,model-associations,associative,Sencha Touch,Has Many,Model Associations,Associative,我试图在SenchaTouch2.3.1中创建一个简单的模型关联 基本上我想有一个商店的优惠券,按类别排列。所以每个类别都有很多优惠券 我的数据目前在存储声明中硬编码,没有代理或服务器 类别模型声明: Ext.define('Category', { extend: 'Ext.data.Model', config: { idProperty: 'id', fields: [ { name: 'id'}, { name: 'name' },

我试图在SenchaTouch2.3.1中创建一个简单的模型关联

基本上我想有一个商店的优惠券,按类别排列。所以每个类别都有很多优惠券

我的数据目前在存储声明中硬编码,没有代理或服务器

类别模型声明:

Ext.define('Category', {
extend: 'Ext.data.Model',

config: {
    idProperty: 'id',
    fields: [
        { name: 'id'},
        { name: 'name' },
        { name: 'itemType'}
    ],
    hasMany: [
        {
            associatedModel: 'Coupon',
            name: 'Coupons',
            foreignKey: 'category_id'
        }
    ]
}});
优惠券型号:

Ext.define('Coupon', {
extend: 'Ext.data.Model',
config: {

    fields: [
        { name: 'couponId'},
        { name: 'title'},
        { name: 'description'},
        { name: 'category_id'},
    ],
    belongsTo: [
        {
            model: 'Category',
            name: 'Category',
            associationKey: 'category_id'
        }
    ]
}});
类别存储和数据:

Ext.define('CategoryStore', {
extend: 'Ext.data.Store',
config: {
    autoLoad: true,

    data: [
        {
            id: 1,
            name: "cat1"
        },
        {
            id: 2,
            name: "cat2"
        }
    ],
    model: 'Category',
    storeId: 'CategoryStore'
}});
优惠券店:

Ext.define('CouponStore', {

extend: 'Ext.data.Store',
config: {
    autoLoad: true,

    data: [
        {
            id: '1',
            title: 'coupon1',
            description : 'some desc',
            category_id:1
        },
        {
            id: '2',
            title: 'coupon2',
            description : 'desc2',
            category_id:2
        }
    ],
    model: 'Coupon',
    storeId: 'CouponStore'
}
})

最后,app.js中启动函数中的代码:

Ext.application({

name: 'hasmany',

launch: function () {
    // Destroy the #appLoadingIndicator element
    Ext.fly('appLoadingIndicator').destroy();

    var categoryStore = Ext.create('CategoryStore');

    var cat2 = categoryStore.findRecord('name','cat2');

    console.log(cat2.getData()); //This works fine

    console.log(cat2.Coupons().getData()); //This returns an empty array
}
})

结果是: hasMany关联方法cat2。优惠券返回空数组:

Chrome控制台:

> Object {id: 2, name: "cat2", itemType: undefined} app.js:100
Class {all: Array[0], items: Array[0], keys: Array[0], indices: Object, map: Object…}
任何帮助都将不胜感激,谢谢

*编辑: 我通过一种变通方法实现了预期的结果:

var couponStore = Ext.create('CouponStore');
couponStore.filter('category_id',2);

因此,目前这对我来说已经足够好了,但是我很想知道为什么我以前的代码不起作用。

我遇到了同样的问题。基本上,您需要为任何hasMany关联创建一个空白关联,因为您的cat2.优惠券返回hasMany存储,但是如果没有关联的记录,它只会返回一个.recordCount=0的存储。因此,您需要添加一个空白关联。我现在让它部分工作。我所做的是在解析传入数据时添加以下代码块,我更改了变量名以匹配您的名称:

    var recArray;
    var couponRecs = category.Coupons();
    var couponModel = Ext.ModelManager.getModel('YourAppName.model.Coupon');

    if (couponRecs.getCount() === 0){
        recArray = couponnRecs.add(couponModel);
        recArray[0].set({attributes here});
    }
补充:


我让这个工作,但有一个问题,在许多协会不坚持。请参见

通过定义存储中的数据字段,可能不会创建关联。试试这个:

var cat1 = Ext.create('Category', {
    id: 1,
    name: 'cat1',
    Coupons: [
        {
            id: '1',
            title: 'coupon1',
            description : 'some desc',
            category_id:1
        },
        {
            id: '2',
            title: 'coupon2',
            description : 'desc2',
            category_id:1
        }
    ]
});
console.log(cat1.Coupons().getData());
或者可以使用.getCount方法代替.getData

答案最终将是2