我正在使用Backbone.js,我有很多事件,它们会在我的路由器中产生Options对象的设置。被调用的视图需要这些对象,因此,它们必须在创建视图之前完成。问题是发生的这些事件是ajax并且是异步的,因此它们在显示视图之前不会完成。我尝试使事件同步,但这导致其他发布,如冻结gui。所以,我正在尝试链接我的函数,以便在调用所有函数之后创建视图。但是,这对我不起作用,因为我似乎无法弄清楚如何在延迟调用之间传递数据。这就是我所拥有的:
Router.js:
someParentFunction:function(paramA, paramB){
var that = this;
var defer1 = $.when(
$.get(that.functionA('somedata1','somedata2',that))
);
defer1.done(function () {
var defer2 = $.when(
$.get(that.functionB('someData',that))
);
defer2.done(function (data) {
var defer3 = $.when(
$.get(that.functionC('somedata1',that))
);
defer3.done(function (data) {
//how do I get the results from each Deferred function?
//keeling in mind that each deferred function
//also receives parameters.
//also, the order of the other functions does not matter,
//as long as they all return their values before this
//view is created.
that.view = new ProjectView({
someParam1:paramA,
someParam2:paramB,
resultsA: jQuery.parseJSON(defer1.results),
resultsB: jQuery.parseJSON(defer2.results),
resultsC: jQuery.parseJSON(defer3.results),
}),
window.app.page(that.view, {
tab:'someName',
})
});
});
});
}
functionA: function(param1, param2){
var url = '?q=somestring&' + param1 + '&' + param2 ;
return $.ajax({
url: url,
context: context,
beforeSend: function( xhr ) {
);
}
}).success(function( data ) {
}).responseText;
},
functionB: function(param1, context){
var url = '?q=somestring&' + param1 ;
return $.ajax({
url: url,
context: context,
beforeSend: function( xhr ) {
);
}
}).success(function( data ) {
}).responseText;
},
functionC: function(param1, context){
var url = '?q=somestring&' + param1;
return $.ajax({
url: url,
context: context,
beforeSend: function( xhr ) {
);
}
}).success(function( data ) {
}).responseText;
},
答案 0 :(得分:0)
经过一段时间的努力,这就是我发现的工作:
functionA: function(param1, context){
var url = '?q=myApp/api/general_functions/¶m1=' + param1;
return $.ajax({
url: url,
context: context,
beforeSend: function( xhr ) {
}
}).success(function( data ) {
});
},
然后每个函数的结构都是这样的:
public assignment1st()
{
super("create student file");
try{
output=new DataOutputStream(new FileOutputStream("studentRec.dat"));
}
catch ( IOException e ) {
System.err.println( "File won't open properly/n" +
e.toString( ) );
System.exit( 1 );
}
initialize();
//*******HERE STARTS THE COUNTRY/STATE COMBOBOX BUILD**************************************
String[] countries = {"-CHOOSE","Australia","Belgium","Brazil","Canada","Georgia","Greece",
"India","Lithuania","Macedonia"};
comboBox_1 = new JComboBox<Object>(countries);
comboBox_1.addActionListener(this);
comboBox_1.setBounds(278, 142, 92, 20);
frame.getContentPane().add(comboBox_1);
// Create sub combo box with multiple models
//State Combobox
comboBox_2 = new JComboBox<String>();
comboBox_2.addItem("-CHOOSE-");
comboBox_2.setBounds(452, 142, 109, 20);
frame.getContentPane().add(comboBox_2);
comboBox_2.setPrototypeDisplayValue("XXXXXXXXXX");
String[] Australia = { "New South Wales", "Tasmania", "Queensland" ,"Victoria"};
states.put(countries[1], Australia);
String[] Belgium = { "Louxembourg", "Hainaut", "Flemish" };
states.put(countries[2], Belgium);
String[] Brazil = { "Amazonas", "Mato Grosso" };
states.put(countries[3], Brazil);
String[] Canada = { "Vancouver", "Quebec" };
states.put(countries[4], Canada);
String[] Georgia = {"Tbilisi", "S.Ossetia" };
states.put(countries[5], Georgia);
String[] Greece = { "Pelloponisos", "Chalchidikis", "Thesprotias" };
states.put(countries[6], Greece);
String[] India = { "Jalpur", "Kolkata", "New Delhi" };
states.put(countries[7], India);
String[] Lithuania = { "Akmene", "Kretinga", "Varena" };
states.put(countries[8],Lithuania);
String[] Macedonia = { "Bitola", "Struga", "Veles" };
states.put(countries[9], Macedonia);
}
这确保了包含在.when方法中的每个函数在到达.done方法之前完成。
希望这有助于其他任何人。