Reputation: 1486
This works fine
class Window.AppViewModel
message : ko.observable("")
chatMessages : ko.observableArray()
canSendMessage : ko.observable(false)
Window.sHub.addMsg = (data) =>
@::chatMessages.push(data)
whereas this
class Window.AppViewModel
constructor : ->
@message = ko.observable("")
@chatMessages = ko.observableArray()
@canSendMessage = ko.observable(false)
Window.sHub.addMsg = (data) =>
@chatMessages.push(data)
Results in my server side hub recieving server messages but it can't make any callbacks.
What am I missing here??? This is driving me up the wall. Obviously the prototype based solution will work because it's making everything "static" but I'm crippled because I can't write nicely separated code at the moment.
Is there any way to see what the hub is trying to invoke?? The SignalR documentation talks about tracing/logging but never explains how.
Upvotes: 0
Views: 309
Reputation: 1486
All wired client events can be found by adding a small snippet to the "On" anonymous function in jquery.SignalR in the hubProxy prototype
So now it looks something like this
on: function (eventName, callback) {
/// <summary>Wires up a callback to be invoked when a invocation request is received from the server hub.</summary>
/// <param name="eventName" type="String">The name of the hub event to register the callback for.</param>
/// <param name="callback" type="Function">The callback to be invoked.</param>
var self = this;
// Normalize the event name to lowercase
eventName = eventName.toLowerCase();
console.log(eventName + " callback was registered on the client");
$(self).bind(eventNamespace + eventName, function (e, data) {
callback.apply(self, data);
});
self.subscribed = true;
return self;
}
And gives you nice output of what client methods can be called back from the server
Also, I learned that CreateHubProxies (reflects through the callbacks you add) is only called once for an entire hub.Start()--So all methods must be added to the hub proxy before you start.
This should definitely be on the github wiki. It makes sense but it isn't obvious.
Edit : It would make sense to allow you to call CreateHubProxies multiple times. People are accustom to writing Knockout.js code in a way where they delay allocating a viewmodel until a certain event happens--if your viewmodel has any SignalR callbacks, this isn't impossible. Having to create all of your viewmodels upfront is a bit of a kludge. You could even keep it pretty lean by keeping a list of proxy members already generated and excluding them.
Upvotes: 1