Commit 4c1d8790a8daf158b0f4278ab2fe0eaa12f183f3

Authored by Adhidarma Hadiwinoto
1 parent 786b10c734
Exists in master

json.stringify

Showing 2 changed files with 16 additions and 17 deletions Side-by-side Diff

... ... @@ -42,52 +42,52 @@ app.engine('.json', whiskers.__express);
42 42 app.set('views', __dirname+'/views');
43 43  
44 44 function getBalances(suppliers, thousands, callback) {
45   -
  45 +
46 46 var kw = [];
47 47 var count = suppliers.length;
48   -
  48 +
49 49 for (var i=0; i < count; i++) {
50 50 var supplier = suppliers[i];
51 51 kw.push('balance.gw:' + supplier);
52 52 }
53   -
  53 +
54 54 redis.mget(kw, function(err, res) {
55 55 var balances = [];
56   -
  56 +
57 57 for (var i=0; i<count; i++) {
58 58 var value = res[i];
59 59 if (!value) {
60 60 value = 0;
61 61 }
62   -
  62 +
63 63 if (thousands) {
64 64 value = numeral(res[i]).format('0,0');
65 65 }
66   -
  66 +
67 67 var url = null;
68 68 var supplier = suppliers[i];
69   -
  69 +
70 70 if (config.expresso_urls[supplier]) {
71 71 url = config.expresso_urls[supplier];
72 72 }
73   -
  73 +
74 74 balances.push({
75 75 title: supplier,
76 76 balance: value,
77 77 url: url,
78 78 });
79 79 }
80   -
  80 +
81 81 callback(null, balances);
82 82 });
83 83 }
84 84  
85 85 app.get('/', function(req, res) {
86   -
  86 +
87 87 Sync(function() {
88 88 var template_file;
89 89 var thousands = true;
90   -
  90 +
91 91 res.format({
92 92 html: function() {
93 93 template_file = 'index.html';
... ... @@ -102,16 +102,17 @@ app.get(&#39;/&#39;, function(req, res) {
102 102 'default': function() {
103 103 template_file = 'index.html';
104 104 },
105   -
  105 +
106 106 });
107   -
  107 +
108 108 var balances = getBalances.sync(null, suppliers, thousands);
109 109 //balances = getBalances(suppliers);
110   -
  110 +
111 111 res.render(template_file, {
112 112 logo: 'http://reload97.com/sites/default/files/web-reload97.png',
113 113 title: 'Supplier Balances',
114 114 balances: balances,
  115 + json_string: JSON.stringify(balances),
115 116 timestamp: new Date(),
116 117 });
117 118 })
1   -{
2   -{for balance in balances}"{balance.title}": {balance.balance},
3   -{/for}}
  1 +{balances}