SignalR & require.js configuration

2020-05-21 04:27发布

I'm incorporating SignalR into a project where I'm already using require.js to handle my scripts dependencies.

I'm having a little trouble making sure "/signalr/hubs" is called after "jquery.signalR-1.1.2" loads.

I got it to work, but I'm wondering if there is a better alternative out there.

This is what I have:

require(["signalr"], function () {
  require(["noext!/signalr/hubs"], function () {
      //initialize and work with the hub here
  }
}

Is there a way I can create a shim here to establish the dependency between signalr/hubs and the signalr script?

Thanks!

1条回答
干净又极端
2楼-- · 2020-05-21 04:46

This works for me with SignalR 1.1.2:

require.config({
baseUrl: "/<your scripts dir>",
paths: {
    "jquery": "jquery-<your jquery version>.min",
    "signalr.core": "jquery.signalR-<your signalr version>.min",
    "signalr.hubs": "/signalr/hubs?"
},
shim: {
    "jquery": {
        exports: "$"
    },
    "signalr.core": {
        deps: ["jquery"],
        exports: "$.connection"
    },
    "signalr.hubs": {
        deps: ["signalr.core"],
    }
}
});

require(["jquery", "signalr.hubs"],
    function($)
    {
        var hubProxy = $.connection.myHub;

        // ... go to town ...
    });
查看更多
登录 后发表回答