问题
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)
- Always put your Proxies in your Models, not your Stores, unless you have a very good reason not to *
- Always require your child models if using them in hasMany relationships. **
- Always use foreignKey if you want to load the children at will
- Always use associationKey if you return the children in the same response as the parent
- You can use both foreignKey and associationKey if you like
- Always name your hasMany relationships
- Always use fully qualified model names in your hasMany relationship
- Consider giving the reader root a meaningful name (other than "data")
- 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