Extjs 4.1 many-to-many model association

I'm having some problem when I want to create a many-to-many association. because he does not know. I am trying to create a relationship this way. but I'm not sure. can anyone 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'}] 

});

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

});

+4
source share
1 answer

First, please describe what does not work? Are magic methods created by hasMany relationships inaccessible to the model? Is data loading when you call the magic method? Please further describe the problems you are experiencing.

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

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

  • Always place your proxies in your models, not in your stores, unless you have good reason not to *
  • Always require your child models to use them in a hasMany relationship. **
  • Always use foreignKey if you want to load children at will.
  • Always use connectionKey if you are returning children in the same answer as the parent
  • You can use both foreignKey and associationKey if you want
  • Always indicate your relationship hasMany
  • Always use fully qualified model names in your hasMany relationship.
  • Consider giving the reader the root of a meaningful name (other than "data").
  • Child model does not need toIt to hasMany relation to work

* The store inherits its model proxy server, and you can always override it

** To simplify the task and avoid potential circular links, you can require them in app.js

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

+2
source

All Articles