Extjs 4.1 many-to-many model association

蹲街弑〆低调 提交于 2019-12-09 13:26:43

问题


i face some problem when i want create many-to-many association. because it do not know. i try to create relationship in this way. but i am not sure. any man can help me? how can i do it?. here is my code

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

});

2nd model is here

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

});


回答1:


First, please describe what isn't working? Are the magic methods created by the hasMany relationship not available on the model? Is data not loading when you call the magic method? Please further describe the issues you are experiencing.

Perhaps you will find this blog post useful? http://extjs-tutorials.blogspot.com/2012/05/extjs-hasmany-relationships-rules.html

Some important tip from the blog post (in case the link dies at some point)

  1. Always put your Proxies in your Models, not your Stores, unless you have a very good reason not to *
  2. Always require your child models if using them in hasMany relationships. **
  3. Always use foreignKey if you want to load the children at will
  4. Always use associationKey if you return the children in the same response as the parent
  5. You can use both foreignKey and associationKey if you like
  6. Always name your hasMany relationships
  7. Always use fully qualified model names in your hasMany relationship
  8. Consider giving the reader root a meaningful name (other than "data")
  9. The child model does not need a belongsTo relationship for the hasMany to work

*The store will inherit its model's proxy, and you can always override it

**To make it easy, and avoid potential circular references, you can require them in app.js

Credit: Neil McGuigan (http://www.blogger.com/profile/14122981831780837323)



来源:https://stackoverflow.com/questions/13852679/extjs-4-1-many-to-many-model-association

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!