PDA

View Full Version : load hasMany associated data in separate request



Arch Gentoo
25 Nov 2014, 4:48 AM
Hello,


I have a little problem with hasMany association and I can't find the possible solution on web. I have two model definitions and I dont want to load them in a single request, so i configured proxies to both models:



Ext.define("Models.Authorization.Groups", {
"extend": "Ext.data.Model",
"fields": [
{"name":"id","type":"int"},
{"name":"name","type":"string"},
{"name":"type","type":"string"}
],
"idProperty":"id",
"proxy":{
"type":"ajax",
"api": {
"read":"\/extphp\/models\/Authorization\/Groups\/read.js",
"create":"\/extphp\/models\/Authorization\/Groups\/create.js",
"update":"\/extphp\/models\/Authorization\/Groups\/update.js",
"destroy":"\/extphp\/models\/Authorization\/Groups\/destroy.js"
},
"reader":{
"type":"json",
"root":"data",
"idProperty": ["id"],
"messageProperty":"message",
"successProperty":"success",
"totalProperty":"total"
},
"writer":{
"type":"json",
"writeAllFields":false,
"root":"data"
}
}
});




Ext.define("Models.Authorization.Users", {
"extend":"Ext.data.Model",
"fields":[
{"name":"id","type":"int"},
{"name":"login","type":"string"},
{"name":"lastLogin","type":"datetime"}
],
"idProperty":"id",
"proxy":{
"type":"ajax",
"api":{
"read":"\/extphp\/models\/Authorization\/Users\/read.js",
"create":"\/extphp\/models\/Authorization\/Users\/create.js",
"update":"\/extphp\/models\/Authorization\/Users\/update.js",
"destroy":"\/extphp\/models\/Authorization\/Users\/destroy.js"
},
"reader":{
"type":"json",
"root":"data",
"idProperty":["id"],
"messageProperty":"message",
"successProperty":"success",
"totalProperty":"total"
},
"writer":{
"type":"json",
"writeAllFields":false,
"root":"data"
}
},
"hasMany":[
{
"name":"groups",
"associationKey":"groups",
"foreignKey":"groups",
"model":"Models.Authorization.Groups"
},
{
"name":"permissions",
"associationKey":"permissions",
"foreignKey":"permissions",
"model":"Models.Authorization.Permissions"
}
],
});


So now I have this store with such trivial code:




var store = Ext.create('Ext.data.Store', {
model: "Models.Authorization.Users",
autoSync: true,
autoLoad: false
});


So basically what I wan't to do is to feed this store with only Models.Authorization.Users and for a single row associated data in another request. The first request works perfectly:




{
"total":1,
"message":"",
"success":true,
"data":[
{"id":2,"mainGroup_id":5,"login":"somelogin","lastLogin":"czwartek, 02.10.2014 12:22:02"}
]
}


As I mentioned - I don't want to load groups in this request! So then I'm trying to execute:

var g = store.getAt(0).groups();
g.load({ callback: function(records, operation){
console.log(store.data.items); // EMPTY!
}});


So the console gives me empty array [] and I have no idea why. I see the request going and I see records as a RAW record data (see attachment) BUT the store "[g]" is always empty! I've tried doing things like store.loadRecords(records); but the result is always the same.

What am I doing wrong?

joel.watson
25 Nov 2014, 11:48 AM
Hi Arch Gentoo--

In order for this work, you'll need to make a few modifications:

Add the "user" foreign key to your group JSON response
Add the "user" foreign key field to your group model definition
Add the foreign key to your hasMany definition for groups
Here's a fiddle demonstrating: https://fiddle.sencha.com/#fiddle/e0i

Hope that helps!

Arch Gentoo
26 Nov 2014, 2:26 AM
Thanks a lot Joel, I've implemented the changes u suggested and now it works like a charm. That's another big step for my wrapper for dynamic models (Doctrine ExtJS)! :)

joel.watson
26 Nov 2014, 6:39 AM
Thanks a lot Joel, I've implemented the changes u suggested and now it works like a charm. That's another big step for my wrapper for dynamic models (Doctrine ExtJS)! :)

Great, glad it's working!

Is this wrapper something you'll be able to share with the community when it's completed?

Thanks!

Arch Gentoo
26 Nov 2014, 11:25 PM
Well, unfortunately our company legal notes doesn't allow to share the code. I'll be still working on this within my custom engine but it won't be a stand-alone functionality (most likely a core part of an engine).