Versions Compared

Key

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

...

Code Block
languagexml
<div class="data-panel">
    <table>
        <tr>
            <td> Number </td>
            <td class="number"> {{data.number}} </td>
        </tr>
        <tr>
            <td> Square </td>
            <td class="number"> {{data.square}} </td>
        </tr>
        <tr>
            <td> Cube </td>
            <td class="number"> {{data.cube}} </td>
        </tr>
    </table>

    <p>
        A message from our sponsors:
    </p>
    <p>
        <span class="quote"> {{data.message}} </span>
    </p>
</div>

...

Code Block
languagejs
// injected refs
var $log, $scope, wss, ks;

// constants
var dataReq = 'sampleCustomDataRequest',
    dataResp = 'sampleCustomDataResponse';
Warning

Still WIP, sorry....

...

Defining the Controller 

We'll skip over the helper functions for the moment and focus on the controller:

Code Block
languagejs
angular.module('ovSampleCustom', [])
    .controller('OvSampleCustomCtrl', 
    [ '$log', '$scope', 'WebSocketService', 'KeyService',
 
    function (_$log_, _$scope_, _wss_, _ks_) { 
        ...
    }]);

The first line here gets a reference to the "ovSampleCustom" module. Again, this is the naming convention in play; the module name should start with "ov" (lowercase) followed by the identifier for our view, in continuing camel-case.

The controller() function is invoked on the module to define our controller. The first argument – "OvSampleCustomCtrl" – is the name of our controller, as registered with angular. Once again, the naming convention is to start with "Ov" followed by the identifier for our view (continuing camel-case), followed by "Ctrl". The second argument is an array...

All the elements of the array (except the last) are the names of services to be injected into our controller at run time. Angular uses these to bind the actual services to the specified parameters of the controller function (the last item in the array).

 

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_;
$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
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);
}

The map entries use well-known logical keys to identify the keystroke (in this example 'space' for the space-bar), with a value being a two-element array defining the callback function and the text to display in the Quick Help panel.

The _helpFormat key is a special value that is used by the Quick Help Service to lay out the definition of bound keystrokes; each sub-array in the main array is a "column" in the panel; the entries in each sub-array define the order in which the keys are listed.

Info

See the Key Service for more information about binding keystrokes to actions, and for further information on the _helpFormat array.

The getData() function tells the WebSocketService to send the data request event to the server:

Code Block
languagejs
function getData() {
    wss.sendEvent(dataReq);
}

 

Remember the ng-click attribute in the "button" div in our HTML snippet? Well, here we set the function reference on our scope:

Code Block
languagejs
// custom click handler
$scope.getData = getData;

 

To populate our view the first time it is loaded, we need to send an initial event to the server:

Code Block
languagejs
// get data the first time...
getData();

 

When our view is destroyed (when the user navigates to some other view), we need to do some cleanup:

Code Block
languagejs
// cleanup
$scope.$on('$destroy', function () {
    wss.unbindHandlers(handlers);
    ks.unbindKeys();
    $log.log('OvSampleCustomCtrl has been destroyed');
});

 

The last thing the controller does when it is initialized is to log to the console:

Code Block
languagejs
 $log.log('OvSampleCustomCtrl has been created');

sampleCustom.css

Glue Files

...