I have a script that loads additional data and gives extra functionality to multiple elements of a view. I like to take a more OPP aproach to do this so i have "classes" build for my entities. The problem i am facing right now is that the informations comes from server side so I need that data before I can give functionality.
On my script I have three Objects User, Prospect and ProspectDetailView.
so I work in this way.
$(document).ready(function _document_ready()
{
//*
ajaxStatus.showStatus('Cargando información adicional...');
var promotor = new User();
var prospecto = new Prospect($("input[name=record]").val());
var vistaDetalle = new ProspectDetailView();
vistaDetalle.drawAditionalStatusTag(prospecto.data);
vistaDetalle.drawGroupDataField(prospecto.inscription);
vistaDetalle.drawInscriptionDifferences(prospecto.data, prospecto.inscription);
vistaDetalle.drawPendingRequestTag(prospecto.inscription.requests);
vistaDetalle.drawPendingTicketTag(prospecto.inscription.tickets);
vistaDetalle.drawTicketsHistoryPanel(prospecto.inscription.tickets_h);
ajaxStatus.hideStatus();
//*/
});//#END _document_ready()
As you can see create a User then a Prospect and finaly load my View Object.
Problem is that I need the data from the prospect (mandatory) before I can work with the view.
I load my Prospect like this
// Definiciones de la información del prospecto.
var Prospect = function (uuid){
this.uuid = uuid;
var __init__ = function (self)
{
$.ajax({
type : "POST",
dataType: "json",
data : {"confirm" : true },
"context": self,
"async" : false,
url : dev()+'/crmutilidades/get_inscription_data/'+self.uuid+'/'+module_sugar_grp1,
success : function _success_get_inscription_data(response)
{
this.inscription = response.data;
this.data = response.data.prospect;
this.paid = response.data.inscrito !== 'undefined' ? response.data.inscrito : false;
}
});
}
__init__(this);
}//#END Prospect
I need to use the async false in order to retrieve the prospect data before i can use it in the view object, and it works, but can't stop the feeling that it's a nasty hack or wrong.
So I tried to use $.when (promises i guess are called)
and did this:
prospect = null;
user = null;
$.when(prospect = new Prospect(), user = new User).then( function(){
view = new ProspectDetailView();
...Do all view calls.
});
Hoping that the view will execute when the prospect and user finish loading but i fail. I get the same error . prospecto is undefined, because everything is async.
How should I model that. As far as I can tell i need to directly pass ajax calls to the $.when method but that would defeat the purpose to have my entities separated and isolated from one another.