@@ -195,20 +195,20 @@ Let's refactor our example and move the currency conversion into a service in an
195
195
<file name="finance2.js">
196
196
angular.module('finance2', [])
197
197
.factory('currencyConverter', function() {
198
- var currencies = ['USD', 'EUR', 'CNY'],
199
- usdToForeignRates = {
198
+ var currencies = ['USD', 'EUR', 'CNY'];
199
+ var usdToForeignRates = {
200
200
USD: 1,
201
201
EUR: 0.74,
202
202
CNY: 6.09
203
203
};
204
+ var convert = function (amount, inCurr, outCurr) {
205
+ return amount * usdToForeignRates[outCurr] / usdToForeignRates[inCurr];
206
+ }
207
+
204
208
return {
205
209
currencies: currencies,
206
210
convert: convert
207
211
};
208
-
209
- function convert(amount, inCurr, outCurr) {
210
- return amount * usdToForeignRates[outCurr] / usdToForeignRates[inCurr];
211
- }
212
212
});
213
213
</file>
214
214
<file name="invoice2.js">
@@ -325,21 +325,15 @@ The following example shows how this is done with Angular:
325
325
var YAHOO_FINANCE_URL_PATTERN =
326
326
'http://query.yahooapis.com/v1/public/yql?q=select * from '+
327
327
'yahoo.finance.xchange where pair in ("PAIRS")&format=json&'+
328
- 'env=store://datatables.org/alltableswithkeys&callback=JSON_CALLBACK',
329
- currencies = ['USD', 'EUR', 'CNY'],
330
- usdToForeignRates = {};
331
- refresh();
332
- return {
333
- currencies: currencies,
334
- convert: convert,
335
- refresh: refresh
336
- };
328
+ 'env=store://datatables.org/alltableswithkeys&callback=JSON_CALLBACK';
329
+ var currencies = ['USD', 'EUR', 'CNY'];
330
+ var usdToForeignRates = {};
337
331
338
- function convert(amount, inCurr, outCurr) {
332
+ var convert = function (amount, inCurr, outCurr) {
339
333
return amount * usdToForeignRates[outCurr] / usdToForeignRates[inCurr];
340
334
}
341
335
342
- function refresh() {
336
+ var refresh = function () {
343
337
var url = YAHOO_FINANCE_URL_PATTERN.
344
338
replace('PAIRS', 'USD' + currencies.join('","USD'));
345
339
return $http.jsonp(url).success(function(data) {
@@ -351,6 +345,14 @@ The following example shows how this is done with Angular:
351
345
usdToForeignRates = newUsdToForeignRates;
352
346
});
353
347
}
348
+
349
+ refresh();
350
+
351
+ return {
352
+ currencies: currencies,
353
+ convert: convert,
354
+ refresh: refresh
355
+ };
354
356
}]);
355
357
</file>
356
358
<file name="index.html">
0 commit comments