PDA

View Full Version : Synchronous association call or something



invalidoyt
20 May 2012, 11:18 AM
Hello all,

Is it possible to make use of associations without making a new call to server? When I try to record.getBelongsToModelName(), i need to run a callback to get the data returned and use it. However, I think the models should be able to prepare the data for the store with the associated data.



userrecord.getCompany(function(company) {
//make use of company
});


instead, I spent some time to accomplish something like this:



var mycompany;

mycompany = userrecord.getCompany(function(company) {
return company;
});


of course, it failed as it waited for the server return. then I tried:



var me = this;
var mycompany;

userrecord.getCompany(function(company) {
mycompany = company;
}, me);


and this:



var me = this;
var mycompany;

userrecord.getCompany(function(company) {
me.mycompany = company;
}, me);


I also tried other methods that I saw in forum posts:



console.log(userrecord.houses()); //has no method, also tried many name conventions


So....

Is there any synchronous way to get the associated model/data? Should I attach the associated data to the child model by defining a "load" callback?

börn
20 May 2012, 11:33 PM
It depends on how much data you load from the server, if you load your parent model... if you have nested data, the associations are filled and you can access these by using yourParentModel.assocName() (giving you a store to the association). If you haven't nested data and want the associations to be lazy loaded, the association stores are there, but not yet filled - so yourParentModel.assocName().load() would load those data and if you need to do something with this loaded data you have to make a callback on success... synchronous wouldn't work there...

if the store to the association is correctly built, you can also access it with .assocNameStore on your model, which is somehow easier in doing console-debugging stuff...

invalidoyt
21 May 2012, 12:17 PM
Thank you for your answer. I wrapped my code inside the callback function for now due to time issues; in the next one, I will try the methods you recommended...