Let's give an example:
The post model
Code:
Ext.define('app.model.Post', { extend:'Ext.data.Model', fields:[ {name:'id', type:'int'}, {name:'title'}, {name:'body'} ], proxy:{ type:'ajax', url:'data/posts.json', reader:{ type:'json', root:'posts' } }, hasMany:[ { name:'categories', model:'app.model.Category' } ] })
The Category model
Code:
Ext.define('app.model.Category', { extend:'Ext.data.Model', fields:[ {name:'id', type:'int'}, {name:'name', type:'string'} ], proxy:{ type:'ajax', url:'data/categories.json', reader:{ type:'json', root:'categories' } } });
The application:
Code:
Ext.Loader.setConfig({ enabled: true }); Ext.application({ name: 'app', autoCreateViewport:false, enableQuickTips:false, requires:[ 'app.model.Post', 'app.model.Category' ], launch: function() { app.model.Post.load(1, { success: function(record, op){ console.log(record.categories()) } }) } });
The above code block is not working. In my opinion it doesn't work because in the second model i am using a second url to "pull" the data. I think extjs needs a format like:
Code:
{ "success":true, "posts":[ { "id":1, "title":"hi", "body":"there", "categories":[ { "id":1, "name":"hardware" }, { "id":3, "name":"software" } ] }, { "id":2, "title":"so", "body":"long", "categories":[ { "id":1, "name":"hardware" }, { "id":2, "name":"" } ] } ] }
The working code is:
The post model
Code:
Ext.define('app.model.Post', { extend:'Ext.data.Model', fields:[ {name:'id', type:'int'}, {name:'title'}, {name:'body'} ], proxy:{ type:'ajax', url:'data/posts.json', reader:{ type:'json', root:'posts' } }, hasMany:[ { name:'categories', model:'app.model.Category' } ] })
The Category model
Code:
Ext.define('app.model.Category', { extend:'Ext.data.Model', fields:[ {name:'id', type:'int'}, {name:'name', type:'string'} ] });
The application:
Code:
Ext.Loader.setConfig({ enabled: true }); Ext.application({ name: 'app', autoCreateViewport:false, enableQuickTips:false, requires:[ 'app.model.Post', 'app.model.Category' ], launch: function() { app.model.Post.load(1, { success: function(record, op){ console.log(record.categories()) } }) } });
and the posts.json file:
Code:
{ "success":true, "posts":[ { "id":1, "title":"hi", "body":"there", "categories":[ { "id":1, "name":"hardware" }, { "id":3, "name":"software" } ] }, { "id":2, "title":"so", "body":"long", "categories":[ { "id":1, "name":"hardware" }, { "id":2, "name":"" } ] } ] }
By using the second (working) code block, I have to pull the whole database in one request! Are there any ideas in how to load what it is really needed?
Thanks