Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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
languagejs
 $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
languagejs
 // 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
languagejs
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
languagejs
addKeyBindings();

.. delegating to another of the previously skipped functions:

Code Block
languagejs
function addKeyBindings() {
    var map = {
        space: [getData, 'Fetch data from server'],
 
        _helpFormat: [
            ['space']
        ]
    };
 
    ks.keyBindings(map);
}

...