简体   繁体   English

如何在执行下一行代码之前等待Backbone事件触发器完成

[英]How to wait for Backbone event trigger to complete before executing next line of code

I have a situation where I open a "connect to something" window, then do a POST request. 我有一种情况,我打开一个“连接到某事”窗口,然后做一个POST请求。

The way I do it is: 我这样做的方式是:

GlobalEventBus.trigger("connection")
doPost()

What happens now is: a connect window opens where I expect the user to enter the credentials and what not but the POST request is fired right after the window gets open. 现在发生的事情是:打开一个连接窗口,我希望用户输入凭据,然后在窗口打开后立即触发POST请求。

I tried to put a while loop to check a boolean variable to see if it is completed, but that while loop gets fired even before the "connection" window gets opened. 我尝试使用while循环检查一个布尔变量以查看它是否已完成,但是即使在“连接”窗口打开之前,while循环也会被触发。

I have no prior experience with UI or JavaScript stuff. 我以前没有UI或JavaScript的经验。 How can I make this work? 我怎样才能做到这一点?

You could use the optional arguments for trigger : 您可以使用可选参数进行trigger

trigger object.trigger(event, [*args]) 触发 object.trigger(event, [*args])
Trigger callbacks for the given event, or space-delimited list of events. 触发给定事件或空格分隔的事件列表的回调。 Subsequent arguments to trigger will be passed along to the event callbacks. 触发器的后续参数将传递给事件回调。

So you could do this: 所以你可以这样做:

GlobalEventBus.trigger("connection", function() { doPost() });
// or
GlobalEventBus.trigger("connection", doPost);

and then the listener for the 'connection' event would grab the function out of its argument list and call it at the appropriate time. 然后'connection'事件的监听器将从其参数列表中获取该函数并在适当的时候调用它。 You'd have to be careful that only one listener called the passed callback function or modify the callback function so that calling it several times would only result in one doPost() call. 您必须要小心,只有一个侦听器调用传递的回调函数或修改回调函数,以便多次调用它只会导致一个doPost()调用。

Alternatively, you could drop the 'connection' event entirely in favor of a simple function that hooks up the requisite callbacks: 或者,您可以完全放弃'connection'事件,转而使用一个简单的函数来连接必需的回调:

app.getConnectionAnd(function() { doPost() });

then getConnectionAnd() would put up the dialog and attach the passed function to its "ok" callback. 然后getConnectionAnd()将建立对话框并将传递的函数附加到其“ok”回调。 You could still fire the events if you had other uses for them but just because you have a general purpose event bus doesn't mean that you have to use it for everything. 如果您对它们有其他用途,您仍然可以触发事件,但仅仅因为您拥有通用事件总线并不意味着您必须将它用于所有事情。

If you really liked Backbone events you could rework your application do avoid this sequence: 如果您真的喜欢Backbone事件,您可以修改您的应用程序,请避免此顺序:

GlobalEventBus.trigger("connection");
doPost();

Instead, you could add another event that triggers doPost : 相反,您可以添加另一个触发doPost事件:

  1. Note somewhere globally accessible that you want to call function() { doPost() } . 请注意您想要调用function() { doPost() }全局可访问的地方。
  2. GlobalEventBus.trigger("connection") is triggered. GlobalEventBus.trigger("connection")
  3. Someone listening for the above throws up the dialog asking for connection details. 听取上述内容的人会抛出要求连接详细信息的对话框。
  4. The dialog establishes the connection and triggers a GlobalEventBus.trigger('connected') event. 该对话框建立连接并触发GlobalEventBus.trigger('connected')事件。
  5. Someone listening for the 'connected' event looks up what is supposed to be called, notices that it is the function from (1), calls it, and resets the function-to-call to undefined . 有人监听'connected'事件会查找应该被调用的内容,注意它是来自(1)的函数,调用它,并将函数调用重置为undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM