【问题标题】:Extjs 4.1 many-to-many model associationExtjs 4.1 多对多模型关联
【发布时间】:2012-12-13 03:55:05
【问题描述】:

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

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'}]

});

【问题讨论】:

    标签: extjs4.1


    【解决方案1】:

    首先,请描述什么不起作用? hasMany 关系创建的魔术方法在模型上不可用吗?调用魔术方法时是否没有加载数据?请进一步描述您遇到的问题。

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

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

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

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

    **为方便起见,并避免潜在的循环引用,您可以在 app.js 中使用它们

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

    【讨论】:

      猜你喜欢
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 2011-09-17
      相关资源
      最近更新 更多