-
20 May 2012 11:18 AM #1
Answered: Synchronous association call or something
Answered: Synchronous association call or something
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.
instead, I spent some time to accomplish something like this:Code:userrecord.getCompany(function(company) { //make use of company });
of course, it failed as it waited for the server return. then I tried:Code:var mycompany; mycompany = userrecord.getCompany(function(company) { return company; });
and this:Code:var me = this; var mycompany; userrecord.getCompany(function(company) { mycompany = company; }, me);
I also tried other methods that I saw in forum posts:Code:var me = this; var mycompany; userrecord.getCompany(function(company) { me.mycompany = company; }, me);
So....Code:console.log(userrecord.houses()); //has no method, also tried many name conventions
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?
-
Best Answer Posted by börn
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...
-
20 May 2012 11:33 PM #2
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...
-
21 May 2012 12:17 PM #3
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...


Reply With Quote