Skip to content

Instantly share code, notes, and snippets.

@cardil
Created January 15, 2016 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cardil/a53629e68e0d35b649fd to your computer and use it in GitHub Desktop.
Save cardil/a53629e68e0d35b649fd to your computer and use it in GitHub Desktop.
Example how to use Eid in Angular 1.x apps (featuring also business filters!)
(function() {
'use strict';
var EidPreconditions = Eid.preconditions;
var checkNotNullable = EidPreconditions.checkNotNullable;
var checkArgument = EidPreconditions.checkArgument;
var tryToExecute = EidPreconditions.tryToExecute;
var app = angular.module('eid-in-ang', []);
var eidifyServiceCall = function(eid, target, operation) {
checkNotNullable(operation, "20160115:124506", 'Operation should not be nullable');
checkNotNullable(target, "20160115:131209", 'Target should not be nullable');
function isIn(operation, target) {
var keys = Object.keys(target);
for (var i = 0; i < keys.length; i++) {
var op = target[keys[i]];
if (op === operation) {
return true;
}
}
return false;
}
checkArgument(isIn(operation, target), "20160115:124636", 'Operation should be a part of target object');
return {
execute: function() {
return tryToExecute(function() {
return operation.apply(target, arguments);
}, eid);
}
}
};
app.service('TickerService', function() {
this.getCurrentQuotes = function() {
return [
{ abbr: "GC.F", name: "Gold Future", value: 1081.10, change: 0.27 },
{ abbr: "CL.F", name: "Crude Oil Future", value: 29.60, change: -5.19 },
{ abbr: "USDPLN", name: "U.S. Dollar / Polish Zloty", value: 4.03608, change: -0.001923 },
{ abbr: "SI.F", name: "Silver Future", value: 1379.50, change: -0.22 },
{ abbr: "EURPLN", name: "Euro / Polish Zloty", value: 4.40668, change: 0.01190 },
{ abbr: "BTCUSD", name: "Bitcoin / U.S. Dollar", value: 407.747, change: -22.416 }
];
};
});
app.filter('cssify', function() {
return function(quote) {
checkNotNullable(quote, '20160115:132101');
checkNotNullable(quote.change, '20160115:132121');
checkArgument(typeof(quote.change) == 'number', '20160115:132140');
var rounded = Math.round(quote.change * 100) / 100;
if (rounded > 0.0) {
return 'raise';
}
if (rounded == 0.0) {
return 'flat';
}
if (rounded < 0.0) {
return 'fall';
}
}
});
app.filter('roundize', function() {
return function(value) {
checkNotNullable(value, '20160115:133944');
checkArgument(typeof(value) == 'number', '20160115:134001');
return Math.round(value * 100) / 100;
}
});
app.controller('TickerCtrl', [ 'TickerService', function(tickerService) {
// If there is a suitable proxy:
// this.quotes = eidify(tickerService, '20160115:122833').getCurrentQuotes();
var quotesGetter = eidifyServiceCall('20160115:122833', tickerService, tickerService.getCurrentQuotes);
this.quotes = quotesGetter.execute();
}]);
})();
<h1>ExceptionID.js used in Angular</h1>
<div ng-app="eid-in-ang">
<div ng-controller="TickerCtrl as ctrl">
<table>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Value</th>
<th>Change</th>
</tr>
<tr ng-repeat="quote in ctrl.quotes" class="{{ quote | cssify }}">
<td>{{ quote.abbr }}</td>
<td>{{ quote.name }}</td>
<td>{{ quote.value }}</td>
<td>{{ quote.change | roundize }}</td>
</tr>
</table>
</div>
</div>
<link rel="stylesheet" href="style.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js"></script>
<script src="//rawgit.com/wavesoftware/javascript-eid-exceptions/v1.0.1/dist/browser/toplevel/eid.min.js"></script>
<script src="eid-in-ang.js"></script>
tr td {
padding: 0.3em;
border: 1px solid gray;
}
tr.raise {
color: green;
}
tr.flat {
color: blue;
}
tr.fall {
color: red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment