...
Inside our controller function, we start by saving injected references inside our closure, so that they are available to other functions. We also initialize our state:
Code Block | ||
---|---|---|
| ||
$log$log = _$log_; $scope = _$scope_; wss = _wss_; ks = _ks_; var handlers = {}; $scope.data = {}; |
...
Next, we need to tell the WebSocketService which callback function to invoke when a "sampleCustomDataResponse" event arrives from the server:
Code Block | ||
---|---|---|
| ||
// data response handler
handlers[dataResp] = respDataCb;
wss.bindHandlers(handlers); |
The callback function (one of the helper methods we skipped earlier) has the job of setting our scope variable data to be the payload of the event, and then prodding angular to re-process:
Code Block | ||
---|---|---|
| ||
function respDataCb(data) { $scope.data = data; $scope.$apply(); } |
Info |
---|
See the Web Socket Service for further details on event handler binding. |
...
Next up, we add our keybindings:
Code Block | ||
---|---|---|
| ||
addKeyBindings(); |
.. delegating to another of the previously skipped functions:
Code Block | ||
---|---|---|
| ||
function addKeyBindings() { var map = { space: [getData, 'Fetch data from server'], _helpFormat: [ ['space'] ] }; ks.keyBindings(map); } |
...