在hub.start()之后检测signalR集线器何时准备就绪 [英] Detecting when signalR hub is ready after hub.start()

查看:107
本文介绍了在hub.start()之后检测signalR集线器何时准备就绪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父子视图模型.父视图模型启动signalR连接:

I have a parent and child viewModel. The parent viewmodel starts the signalR connection:

$.connection.hub.start()

子视图模型-仅在用户访问聊天时才加载的子视图模型-执行以下操作:

The child viewmodel - one that loads only when the user accesses chat - does the following:

chat.server.addUserToChat(self.currentUsername()).done(function() {
    alert('added');
});

问题是,子调用发生在父调用之前.我可以用1秒的setTimeOut来解决此问题,但理想情况下,我可以执行以下操作:

The problem is, the child call is happening before the parent call. I can fix this with a setTimeOut of 1 second, but ideally I could do something like this:

$.connection.hub.ready(function(){chat.server.addUserToChat(self.currentUsername()).done(function() {
        alert('added');
    });});

signalR中是否有类似的东西?还是我需要在视图模型之间使用超时/发布/订阅?

Is there anything like this in signalR? Or do I need to use timeouts / pub/sub between viewmodels?

推荐答案

这似乎更多是结构化应用程序的问题. 您可以将hub.start()返回的延迟对象存储在某些全局对象中,并在子视图模型中访问它:

This seems to be more an issue of structuring your application. You can store the deferred object returned by hub.start() in some global object and access it in your child viewmodel:

window.chatApp = {
    hubConnector: $.connection.hub.start()
};

// in your child viewmodel
chatApp.hubConnector.done(function () {
    chat.server.addUserToChat(self.currentUsername()).done(function () {
        alert('added');
    });
});

这篇关于在hub.start()之后检测signalR集线器何时准备就绪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆