Extjs 4.1 多对多模型关联

2024-04-08

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

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 http://extjs-tutorials.blogspot.com/2012/05/extjs-hasmany-relationships-rules.html

博客文章中的一些重要提示(以防链接在某个时候失效)

  1. 始终将您的代理放在您的模型中,而不是您的商店中,除非您有充分的理由不这样做*
  2. 如果在 hasMany 关系中使用子模型,则始终需要它们。 **
  3. 如果您想随意加载子项,请始终使用foreignKey
  4. 如果您在与父级相同的响应中返回子级,请始终使用关联密钥
  5. 如果您愿意,可以同时使用foreignKey和associationKey
  6. 始终命名您的 hasMany 关系
  7. 始终在 hasMany 关系中使用完全限定的模型名称
  8. 考虑给读者根一个有意义的名称(“数据”除外)
  9. 子模型不需要belongsTo关系来让hasMany工作

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

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

图片来源:尼尔·麦奎根 (http://www.blogger.com/profile/14122981831780837323)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Extjs 4.1 多对多模型关联 的相关文章

随机推荐