extjs4.1 – Extjs 4.1多对多模型关联

当我想要创建多对多关联时,我会遇到一些问题.因为它不知道.我试图以这种方式建立关系.但我不确定.任何人都可以帮助我吗?我该怎么做?.这是我的代码

Ext.define('Ext4Example.model.Category', {
extend    : 'Ext.data.Model',
alias     : 'widget.categoryModel',
idProperty: 'id',    
fields: [
    {name:'id',              mapping:'id',                type:'int'},
    {name:'name',            mapping:'name',              type:'string'}
],
proxy: {
    type: 'ajax',
    noCache: false,
    api: {
        create  : '${createLink(controller:'category', action: 'create')}',
        read    : '${createLink(controller:'category', action: 'read')}',
        update  : '${createLink(controller:'category', action: 'update')}',
        destroy : '${createLink(controller:'category', action: 'destroy')}'
    },
    actionMethods: {
        create  : 'POST',
        read    : 'GET',
        update  : 'PUT',
        destroy : 'DELETE'            
    },        
    reader: {
        type    : 'json',
        root    : 'data',
        totalProperty   : 'total',
        successProperty : 'success',
        messageProperty : 'message',
        implicitIncludes: true
    },
    writer: {
      type      : 'json',
      root      : 'data',
      writeAllFields: true 
    },
    simpleSortMode: true
},
hasMany: [{model: 'Manufacturer', name: 'manufacturers'}]

});

第二个模型在这里

Ext.define('Ext4Example.model.Manufacturer', {
extend    : 'Ext.data.Model',
alias     : 'widget.manufacturerModel',
idProperty: 'id',    
fields: [
    {name:'id',              mapping:'id',                type:'int'},
    {name:'name',            mapping:'name',              type:'string'}
],
proxy: {
    type: 'ajax',
    noCache: false,
    api: {
        create  : '${createLink(controller:'manufacturer', action: 'create')}',
        read    : '${createLink(controller:'manufacturer', action: 'read')}',
        update  : '${createLink(controller:'manufacturer', action: 'update')}',
        destroy : '${createLink(controller:'manufacturer', action: 'destroy')}'
    },
    actionMethods: {
        create  : 'POST',
        read    : 'GET',
        update  : 'PUT',
        destroy : 'DELETE'            
    },        
    reader: {
        type    : 'json',
        root    : 'data',
        totalProperty   : 'total',
        successProperty : 'success',
        messageProperty : 'message',
        implicitIncludes: true
    },
    writer: {
      type      : 'json',
      root      : 'data',
      writeAllFields: true 
    },
    simpleSortMode: true
},
hasMany: [{model: 'Category', name: 'categories'}]

});

最佳答案 首先,请描述一下哪些不起作用? hasMany关系创建的魔术方法在模型上不可用吗?调用魔术方法时数据是否未加载?请进一步说明您遇到的问题.

也许你会发现这篇博文有用吗?
http://extjs-tutorials.blogspot.com/2012/05/extjs-hasmany-relationships-rules.html

博客文章中的一些重要提示(如果链接在某些时候死亡)

>除非你有充分的理由不要将你的Proxies放在你的模特而不是你的商店里,
>如果在hasMany关系中使用它们,则始终需要您的子模型. **
>如果要随意加载子项,请始终使用foreignKey
>如果您在与父项相同的响应中返回子项,请始终使用associationKey
>如果您愿意,可以同时使用foreignKey和associationKey
>始终为您的hasMany关系命名
>始终在hasMany关系中使用完全限定的模型名称
>考虑给读者提供一个有意义的名称(除了“数据”)
>子模型不需要belongsMo关系来使hasMany工作

*商店将继承其模型的代理,您始终可以覆盖它

**为了简化并避免潜在的循环引用,您可以在app.js中要求它们

图片来源:Neil McGuigan(http://www.blogger.com/profile/14122981831780837323)

点赞