I have the following code:
for( let filename of ["action", "course", "program", "staff", "student"]){
http.get('data/'+filename+'.json').map(res => res.json()).subscribe(
staff => localStorage.setItem(filename, JSON.stringify(staff)
));
}
transpiled into :
for (var _i = 0, _a = ["action", "course", "program", "staff", "student"]; _i < _a.length; _i++) {
var filename = _a[_i];
http.get('data/' + filename + '.json')
.map(function (res) { return res.json(); })
.subscribe(function (staff) { return localStorage.setItem(filename, JSON.stringify(staff)); });
}
Which should loop though an array of stings that has the names of files that has json objects that I wish to load in the local storage.
Problem goes as follows:
I only get the value of the last element stored in the local storage. the rest is gone.
reason is:
When the time comes to call the localStorage.setItem
in the .subscribe()
The value stored in filename is equal to the last value in the array.
Ideas I got
- Make The HTTP request synchronous {sure its a bad idea} but the application actually depend on this data thus I don't mind blocking the ui till its done. | anyhow ng2 documentations shows no clue of how to do that...
- use Promises which i have no clue how with angular 2
- find a way to make sure that filename value is some how stored for each loop
- forget about doing it in a loop and write it down hard-coded
my code
app.ts
import {Component, bootstrap, NgFor,CORE_DIRECTIVES,OnInit,provide} from 'angular2/angular2';
import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteParams, RouteData,
LocationStrategy, HashLocationStrategy,Location} from 'angular2/router';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
@Component({
selector: 'app-holder',
template: `<router-outlet></router-outlet>`,
viewProviders: [HTTP_PROVIDERS],
directives: [ROUTER_DIRECTIVES]]
})
export class MainComponent {
constructor(http: Http) {
if(dataStorage.shouldLoad()){
for( filename of ["action", "actiontype", "advisertype", "course", "program", "staff", "student"]){
http.get('data/'+filename+'.json').map(res => res.json()).subscribe(staff => localStorage.setItem(filename, JSON.stringify(staff)));
}
}
}
}
bootstrap(MainComponent,[ROUTER_PROVIDERS, provide(LocationStrategy, {useClass: HashLocationStrategy})]);
index.html
<html>
<head>
<title>Star App</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="../node_modules/systemjs/dist/system.src.js"></script>
<script src="../node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="../node_modules/angular2/bundles/router.dev.js"></script>
<script src="../node_modules/angular2/bundles/http.dev.js"></script>
<script>
System.config({
packages: {'app': {defaultExtension: 'js'}}
});
System.import('app/app');
</script>
</head>
<body>
<app-holder>Please wait while Loading...</app-holder>
</body>
</html>
backGround checkes:
- how to use http in ng2 https://angular.io/docs/ts/latest/api/http/Http-class.html
- solving similer issue but with Angular 1 : how to make synchronous http request in angular js
- options that can be set to http calls with ng2 : https://fetch.spec.whatwg.org/#requestmode